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;
/** The “content type settings” label. */
private I18nLabel contentTypeSettingsLabel;
-
+
/** The “content type default” label. */
private I18nLabel contentTypeDefaultLabel;
/**
* {@inheritDoc}
*/
+ @SuppressWarnings("synthetic-access")
public void actionPerformed(ActionEvent actionEvent) {
boolean insertOverride = insertOverrideCheckBox.isSelected();
List<ProjectFileWrapper> selectedProjectFileWrappers = getSelectedProjectFileWrappers(true);
/**
* @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<ProjectFileWrapper> 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<ProjectFileWrapper> 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);
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();
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. */
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<String> allMimeTypes = MimeTypes.getAllMimeTypes();
List<String> 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
//
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();
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) {
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 */
setEnabled(insertOverrideAction, insertOverrideEnabled);
setSelected(insertOverrideCheckBox, insertOverrideSelected);
contentTypeDefaultTextField.setText(defaultContentType);
+ overrideContentTypeDefaultAction.setEnabled(overrideContentTypeEnabled);
+ overrideContentTypeDefaultCheckBox.setSelected(overrideContentTypeSelected);
+ contentTypeOverrideComboBox.setEnabled(contentTypeOverrideEnabled);
+ contentTypeOverrideComboBox.setSelectedItem(contentTypeOverride);
}
//
}
- private class MimeTypeListModel implements ComboBoxModel {
-
- private List<ListDataListener> listDataListeners = new ArrayList<ListDataListener>();
- 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);
+ }
}
-
+
}