📄 selectconfigurationfilepage.java
字号:
package net.sf.dz.util.wizard;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.event.ActionEvent;import java.awt.event.ItemEvent;import java.io.IOException;import java.io.File;import javax.swing.ButtonGroup;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JPanel;import javax.swing.JRadioButton;import org.freehold.jukebox.logger.Logger;public class SelectConfigurationFilePage extends NarrowWizardPage { private JRadioButton newRadio; private JRadioButton oldRadio; private ButtonGroup sourceGroup; private JButton configFileButton; private JFileChooser configFileChooser; private File suggestedDir; private String suggestedName; private ConfigurationReader cfReader; private String contextKey; /** * Create an instance. * * @param owner Wizard this page belongs to. * * @param suggestedDir Suggested directory name to start from. * * @param suggestedName Suggested configuration file name. * * @param cfReader Configuration reader that will be used to parse the * configuration. * * @param contextKey A key to use to put the parsed configuration into * the context. */ public SelectConfigurationFilePage(Logger logger, Wizard owner, File suggestedDir, String suggestedName, ConfigurationReader cfReader, String contextKey) { super(logger, owner, "Select Configuration File Location"); this.suggestedDir = suggestedDir; this.suggestedName = suggestedName; this.cfReader = cfReader; this.contextKey = contextKey; checkConstructorArguments(); GridBagLayout layout = new GridBagLayout(); GridBagConstraints cs = new GridBagConstraints(); getNarrowPane().setLayout(layout); newRadio = new JRadioButton("Create new configuration"); oldRadio = new JRadioButton("Modify existing configuration"); sourceGroup = new ButtonGroup(); sourceGroup.add(newRadio); sourceGroup.add(oldRadio); JPanel fillerTop = new JPanel(); JPanel fillerBottom = new JPanel(); configFileButton = new JButton(new File(suggestedDir, suggestedName).toString()); configFileButton.addActionListener(this); configFileChooser = new JFileChooser(); cs.fill = GridBagConstraints.BOTH; cs.gridx = 1; cs.gridy = 0; cs.weightx = 1; cs.weighty = 1; cs.gridwidth = 1; layout.setConstraints(fillerTop, cs); getNarrowPane().add(fillerTop); cs.gridy++; cs.fill = GridBagConstraints.HORIZONTAL; cs.weighty = 0; layout.setConstraints(oldRadio, cs); getNarrowPane().add(oldRadio); cs.gridy++; layout.setConstraints(newRadio, cs); getNarrowPane().add(newRadio); cs.gridy++; cs.gridx = 0; cs.gridwidth = 3; layout.setConstraints(configFileButton, cs); getNarrowPane().add(configFileButton); cs.gridy++; cs.fill = GridBagConstraints.BOTH; cs.weighty = 1; layout.setConstraints(fillerBottom, cs); getNarrowPane().add(fillerBottom); newRadio.setSelected(true); newRadio.addItemListener(this); oldRadio.addItemListener(this); } /** * Check if the constructor arguments are sane. * * @exception IllegalArgumentException if they are not. */ private void checkConstructorArguments() { // VT: FIXME: Have to see if the suggested directory and file name // are sane, in particular, file name should be a valid file name // for a local filesystem. if ( contextKey == null ) { throw new IllegalArgumentException("contextKey can't be null"); } } public String getHelpURL() { return "FIXME"; } public String validate() { try { // We can go next if one of the following is true: // // - New file is selected, it is a directory, the directory is // writable; // - Old file is selected, file exists and is writable. File configFile = new File(configFileButton.getText()); if (newRadio.isSelected() ) { // Have to check the directory, not the file. It is // assumed that the configFile will always be a file // name, not the directory name. File target = configFile.getParentFile(); if ( target.exists() && target.isDirectory() && target.canWrite() ) { return ""; } else { return target.toString() + ": doesn't exist, not a directory or not writable"; } } else if ( oldRadio.isSelected() ) { if ( configFile.exists() && !configFile.isDirectory() && configFile.canRead() && configFile.canWrite() ) { // At this point, we're supposed to have put the object // corresponding to the configuration read into the // context. If this object is a Throwable, or null, it // means that the attempt to read the old configuration // failed, for whatever reason. We can't proceed. Object cf = getOwner().getContext().get(contextKey); if ( cf == null ) { if ( cfReader == null ) { return "No configuration reader provided"; } else { return "No configuration could be read from " + configFile + ", see the log"; } } else if ( cf instanceof Throwable ) { return ((Throwable)cf).getMessage(); } return ""; } else { return configFile.toString() + ": doesn't exist, is a directory, not readable or not writable"; } } // If we're here, it means that the radio button deselection // process is in progress. No big deal. return ""; } finally { getOwner().getContext().put("config.file", configFileButton.getText()); getOwner().getContext().put("config.existing", new Boolean(oldRadio.isSelected())); getOwner().getContext().put("config.new", new Boolean(newRadio.isSelected())); } } protected void actionPerformed2(ActionEvent e) { if ( configFileButton.equals(e.getSource()) ) { String buttonText = null; File currentSelection = new File(configFileButton.getText().toString()); if ( !currentSelection.isDirectory() ) { currentSelection = new File(currentSelection.getParent()); } configFileChooser.setCurrentDirectory(currentSelection); if ( newRadio.isSelected() ) { // Must be able to select only directories configFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); buttonText = "Choose this directory"; } else { // Must be able to select only files configFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); buttonText = "Choose this file"; } int rc = configFileChooser.showDialog(getContentPane(), buttonText); if ( rc == JFileChooser.APPROVE_OPTION ) { currentSelection = configFileChooser.getSelectedFile(); if ( currentSelection.isDirectory() ) { currentSelection = new File(currentSelection, suggestedName); } configFileButton.setText(currentSelection.toString()); } // If we want to read the existing configuration, this is a // place to do it. if ( oldRadio.isSelected() && rc == JFileChooser.APPROVE_OPTION ) { // Now we can parse the configuration file and stuff it into the // context. If we fail to do so, we won't be allowed to proceed. if ( cfReader == null ) { complain(LOG_WARNING, CH_WP, "No configuration reader provided"); } else { try { cfReader.read(currentSelection, getOwner().getContext(), contextKey); } catch ( IOException ioex ) { complain(LOG_ERR, CH_WP, "Failed to read the configuration", ioex); getOwner().getContext().put(contextKey, ioex); } } } } } public void itemStateChanged2(ItemEvent e) { // If the radio button is being deselected, there's nothing to do if ( (e.getStateChange() & ItemEvent.DESELECTED) != 0 ) { return; } // If we're trying to select the existing configuration, we need to // try to read it right away, because otherwise we'll be in a funny // state - the display points to a correct location, and the "Next" // button is enabled, but the file won't be read unless we "choose" // it. Object source = e.getSource(); if ( source == oldRadio ) { try { File currentSelection = new File(configFileButton.getText().toString()); complain(LOG_NOTICE, CH_WP, "Reading configuration from " + currentSelection); cfReader.read(currentSelection, getOwner().getContext(), contextKey); } catch ( IOException ioex ) { complain(LOG_ERR, CH_WP, "Failed to read the configuration", ioex); getOwner().getContext().put(contextKey, ioex); } } else if ( source == newRadio ) { cfReader.clear(getOwner().getContext()); // And clear the old configuration from the context as well getOwner().getContext().remove("cf.core"); } } public boolean isEnabled() { return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -