From 431385ab25460a8ef58878c6b109d2a9a5781b37 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 2 Jun 2008 23:24:19 +0200 Subject: [PATCH] finish content type override implementation --- src/net/pterodactylus/jsite/gui/FileManager.java | 162 +++++++++++++++-------- 1 file changed, 106 insertions(+), 56 deletions(-) diff --git a/src/net/pterodactylus/jsite/gui/FileManager.java b/src/net/pterodactylus/jsite/gui/FileManager.java index 6771487..d69d222 100644 --- a/src/net/pterodactylus/jsite/gui/FileManager.java +++ b/src/net/pterodactylus/jsite/gui/FileManager.java @@ -46,19 +46,22 @@ import javax.swing.AbstractAction; import javax.swing.AbstractButton; import javax.swing.Action; import javax.swing.BorderFactory; -import javax.swing.ComboBoxModel; +import javax.swing.DefaultComboBoxModel; +import javax.swing.DefaultListCellRenderer; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JLabel; +import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; +import javax.swing.JSeparator; import javax.swing.JTextField; import javax.swing.JTree; -import javax.swing.event.ListDataListener; +import javax.swing.SwingConstants; import javax.swing.event.TreeModelEvent; import javax.swing.event.TreeModelListener; import javax.swing.event.TreeSelectionEvent; @@ -163,7 +166,7 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe /** The “content type settings” label. */ private I18nLabel contentTypeSettingsLabel; - + /** The “content type default” label. */ private I18nLabel contentTypeDefaultLabel; @@ -365,6 +368,7 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe /** * {@inheritDoc} */ + @SuppressWarnings("synthetic-access") public void actionPerformed(ActionEvent actionEvent) { boolean insertOverride = insertOverrideCheckBox.isSelected(); List selectedProjectFileWrappers = getSelectedProjectFileWrappers(true); @@ -382,18 +386,53 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe /** * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ + @SuppressWarnings("synthetic-access") public void actionPerformed(ActionEvent actionEvent) { - /* TODO */ + boolean contentTypeOverrideEnabled = overrideContentTypeDefaultCheckBox.isSelected(); + contentTypeOverrideComboBox.setEnabled(contentTypeOverrideEnabled); + List selectedProjectFileWrappers = getSelectedProjectFileWrappers(true); + ProjectFileWrapper projectFileWrapper = selectedProjectFileWrappers.get(0); + ProjectFile projectFile = projectFileWrapper.getProjectFile(); + FileOverride fileOverride = project.getFileOverride(projectFile); + if (contentTypeOverrideEnabled) { + if (fileOverride == null) { + fileOverride = new FileOverride(); + project.addFileOverride(projectFile, fileOverride); + } + fileOverride.setContentType(MimeTypes.getMimeType(getFileExtension(projectFile.getName()))); + } else { + fileOverride.setContentType(null); + if (fileOverride.isEmpty()) { + project.removeFileOverride(projectFile); + } + } } }; - overrideContentTypeDefaultAction.setEnabled(false); contentTypeOverrideAction = new AbstractAction() { /** * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ + @SuppressWarnings("synthetic-access") public void actionPerformed(ActionEvent actionEvent) { - /* TODO */ + if (!contentTypeOverrideComboBox.isEnabled()) { + return; + } + String selectedContentType = (String) contentTypeOverrideComboBox.getSelectedItem(); + logger.log(Level.FINEST, "selectedContentType: " + selectedContentType); + if ("--".equals(selectedContentType)) { + return; + } + List selectedProjectFileWrappers = getSelectedProjectFileWrappers(true); + for (ProjectFileWrapper projectFileWrapper : selectedProjectFileWrappers) { + ProjectFile projectFile = projectFileWrapper.getProjectFile(); + FileOverride fileOverride = project.getFileOverride(projectFile); + if (fileOverride == null) { + fileOverride = new FileOverride(); + project.addFileOverride(projectFile, fileOverride); + } + fileOverride.setContentType(selectedContentType); + } } }; contentTypeOverrideAction.setEnabled(false); @@ -499,7 +538,7 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe contentTypeSettingsLabel = new I18nLabel("fileManager.label.contentTypeSetting"); contentTypeSettingsLabel.setFont(contentTypeSettingsLabel.getFont().deriveFont(Font.BOLD)); propertiesPanel.add(contentTypeSettingsLabel, new GridBagConstraints(0, 7, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(24, 0, 0, 0), 0, 0)); - + contentTypeDefaultLabel = new I18nLabel("fileManager.label.contentTypeDefault"); propertiesPanel.add(contentTypeDefaultLabel, new GridBagConstraints(0, 8, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 24, 0, 0), 0, 0)); contentTypeDefaultTextField = new JTextField(); @@ -507,8 +546,10 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe propertiesPanel.add(contentTypeDefaultTextField, new GridBagConstraints(1, 8, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0)); overrideContentTypeDefaultCheckBox = new JCheckBox(overrideContentTypeDefaultAction); propertiesPanel.add(overrideContentTypeDefaultCheckBox, new GridBagConstraints(0, 9, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 24, 0, 0), 0, 0)); - contentTypeOverrideComboBox = new JComboBox(); + contentTypeOverrideComboBox = new JComboBox(new DefaultComboBoxModel()); + contentTypeOverrideComboBox.setRenderer(new MimeTypeComboBoxRenderer()); contentTypeOverrideComboBox.addActionListener(contentTypeOverrideAction); + contentTypeOverrideComboBox.setEnabled(false); propertiesPanel.add(contentTypeOverrideComboBox, new GridBagConstraints(1, 9, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0)); /* glue panel. */ @@ -750,19 +791,29 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe return lastComponent.substring(lastComponent.lastIndexOf('.') + 1); } + /** + * Repopulates the content type combo box with all content types, putting + * the most probably ones to the front of the list. + * + * @param fileName + * The name of the file + */ private void fillComboBox(String fileName) { String fileExtension = getFileExtension(fileName); List allMimeTypes = MimeTypes.getAllMimeTypes(); List eligibleMimeTypes = MimeTypes.getMimeTypes(fileExtension); - for (String mimeType: eligibleMimeTypes) { + for (String mimeType : eligibleMimeTypes) { allMimeTypes.remove(mimeType); allMimeTypes.add(eligibleMimeTypes.indexOf(mimeType), mimeType); } allMimeTypes.add(eligibleMimeTypes.size(), "--"); - allMimeTypes.add(0, null); - contentTypeOverrideComboBox.set + DefaultComboBoxModel contentTypeOverrideComboBoxModel = (DefaultComboBoxModel) contentTypeOverrideComboBox.getModel(); + contentTypeOverrideComboBoxModel.removeAllElements(); + for (String mimeType : allMimeTypes) { + contentTypeOverrideComboBoxModel.addElement(mimeType); + } } - + // // INTERFACE I18nable // @@ -795,6 +846,10 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe boolean insertOverrideEnabled = false; boolean insertOverrideSelected = false; String defaultContentType = MimeTypes.DEFAULT_CONTENT_TYPE; + boolean overrideContentTypeEnabled = false; + boolean overrideContentTypeSelected = false; + boolean contentTypeOverrideEnabled = false; + String contentTypeOverride = "--"; if (selectedProjectFileWrappers.size() == 1) { ProjectFileWrapper projectFileWrapper = selectedProjectFileWrappers.get(0); ProjectFile projectFile = projectFileWrapper.getProjectFile(); @@ -808,6 +863,7 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe fileSizeText = String.valueOf(projectFile.getSize()); insertDefaultSelected = !projectFile.isHidden(); overrideInsertEnabled = true; + overrideContentTypeEnabled = true; defaultContentType = MimeTypes.getMimeType(getFileExtension(projectFile.getName())); FileOverride fileOverride = project.getFileOverride(projectFile); if (fileOverride != null) { @@ -815,7 +871,14 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe overrideInsertSelected = overrideInsert != null; insertOverrideEnabled = overrideInsertSelected; insertOverrideSelected = overrideInsertSelected ? overrideInsert : !projectFile.isHidden(); + String contentType = fileOverride.getContentType(); + if (contentType != null) { + contentTypeOverride = contentType; + overrideContentTypeSelected = true; + contentTypeOverrideEnabled = true; + } } + fillComboBox(projectFile.getName()); } } else if (selectedProjectFileWrappers.size() > 1) { /* TODO */ @@ -829,6 +892,10 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe setEnabled(insertOverrideAction, insertOverrideEnabled); setSelected(insertOverrideCheckBox, insertOverrideSelected); contentTypeDefaultTextField.setText(defaultContentType); + overrideContentTypeDefaultAction.setEnabled(overrideContentTypeEnabled); + overrideContentTypeDefaultCheckBox.setSelected(overrideContentTypeSelected); + contentTypeOverrideComboBox.setEnabled(contentTypeOverrideEnabled); + contentTypeOverrideComboBox.setSelectedItem(contentTypeOverride); } // @@ -1224,55 +1291,38 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe } - private class MimeTypeListModel implements ComboBoxModel { - - private List listDataListeners = new ArrayList(); - private Object selectedItem; - - /** - * @see javax.swing.ListModel#addListDataListener(javax.swing.event.ListDataListener) - */ - public void addListDataListener(ListDataListener listDataListener) { - listDataListeners.add(listDataListener); - } + /** + * A cell renderer for combo boxes that converts the string “--” to a + * separator. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ + private class MimeTypeComboBoxRenderer extends DefaultListCellRenderer { - /** - * @see javax.swing.ListModel#removeListDataListener(javax.swing.event.ListDataListener) - */ - public void removeListDataListener(ListDataListener listDataListener) { - listDataListeners.remove(listDataListener); - } - - /** - * @see javax.swing.ComboBoxModel#getSelectedItem() - */ - public Object getSelectedItem() { - return selectedItem; - } + /** The separator component. */ + private final JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL); /** - * @see javax.swing.ComboBoxModel#setSelectedItem(java.lang.Object) - */ - public void setSelectedItem(Object anItem) { - selectedItem = anItem; - } - - /** - * @see javax.swing.ListModel#getElementAt(int) - */ - public Object getElementAt(int index) { - // TODO Auto-generated method stub - return null; - } + * Empty constructor. + */ + MimeTypeComboBoxRenderer() { + /* do nothing. */ + } /** - * @see javax.swing.ListModel#getSize() - */ - public int getSize() { - // TODO Auto-generated method stub - return 0; - } + * {@inheritDoc} + */ + @Override + public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { + if ("--".equals(value)) { + return separator; + } + if (value == null) { + return super.getListCellRendererComponent(list, "\u00a0", index, isSelected, cellHasFocus); + } + return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); + } } - + } -- 2.7.4