📄 optionsdialog.java
字号:
/*****************************************************************************/
/**
* This method should be overridden to do whatever is necessary to
* "apply" all changes to your application.
*
* @param owner The parent frame you specified in the constructor. You
* can cast this to it's actual type to perform any changes
* necessary.
*/
public abstract void doApplyImpl(Frame owner);
/*****************************************************************************/
/**
* Returns the text on the "Apply" button.
*
* @return The text on the Apply button.
* @see #setApplyButtonText
*/
public final String getApplyButtonText() {
return applyButton.getText();
}
/*****************************************************************************/
/**
* Returns the text on the "Cancel" button.
*
* @return The text on the Cancel button.
* @see #setCancelButtonText
*/
public final String getCancelButtonText() {
return cancelButton.getText();
}
/*****************************************************************************/
/**
* Returns the text on the "OK" button.
*
* @return The text on the OK button.
* @see #setOKButtonText
*/
public final String getOKButtonText() {
return okButton.getText();
}
/*****************************************************************************/
/**
* Returns the panels currently displayable by this options dialog.
*
* @return The options panels.
*/
protected OptionsDialogPanel[] getOptionsDialogPanels() {
return optionsPanels;
}
/*****************************************************************************/
/**
* Initializes all fields/radio buttons/etc. in this options dialog
* with their proper states as obtained from the owner of this options
* dialog (as passed into the constructor). This method should be
* overridden, the parent obtained with <code>getParent</code>, and
* all fields properly initialized. You probably want to call
* <code>setApplyButtonEnabled(false)</code> and
* <code>setUnsavedChanges(false</code> for all Options panels at the end
* of this method as a visual cue to the user that no changes options have
* yet been made.
*/
public abstract void initialize();
/*****************************************************************************/
/**
* Listens for a property change in one of the option panels. This
* basically just listens for the user to change a value, so it can
* activate the "Apply" button.
*/
public void propertyChange(PropertyChangeEvent e) {
if (!applyButton.isEnabled())
applyButton.setEnabled(true);
}
/*****************************************************************************/
/**
* Enables or disables the "Apply" button.
*
* @param enabled Whether or not the Apply button should be enabled.
*/
public void setApplyButtonEnabled(boolean enabled) {
applyButton.setEnabled(enabled);
}
/*****************************************************************************/
/**
* Sets the text on the "Apply" button.
*
* @param text The text to use on the Apply button.
* @see #getApplyButtonText
*/
public void setApplyButtonText(String text) {
applyButton.setText(text);
}
/*****************************************************************************/
/**
* Sets the text on the "Cancel" button.
*
* @param text The text to use on the Cancel button.
* @see #getCancelButtonText
*/
public void setCancelButtonText(String text) {
cancelButton.setText(text);
}
/*****************************************************************************/
/**
* Sets the text on the "OK" button.
*
* @param text The text to use on the OK button.
* @see #getOKButtonText
*/
public void setOKButtonText(String text) {
okButton.setText(text);
}
/*****************************************************************************/
/**
* Sets the options panels available on this options dialog.
*
* @param optionsPanels The options panels to be available.
*/
public void setOptionsPanels(OptionsDialogPanel[] optionsPanels) {
this.optionsPanels = optionsPanels;
int numOptionPanels = optionsPanels.length;
// Create the tree listing the options panels.
root.removeAllChildren();
for (int i=0; i<numOptionPanels; i++) {
OptionsDialogPanel panel = optionsPanels[i];
MutableTreeNode node = new DefaultMutableTreeNode(panel);
treeModel.insertNodeInto(node, root, root.getChildCount());
int childCount = panel.getChildPanelCount();
for (int j=0; j<childCount; j++) {
treeModel.insertNodeInto(new DefaultMutableTreeNode(
panel.getChildPanel(j)),
node, j);
}
}
UIUtilities.expandAllNodes(optionTree);
// Remove old option panels and add new ones.
currentOptionPanel.removeAll();
for (int i=0; i<numOptionPanels; i++) {
addOptionPanel(optionsPanels[i]);
int childCount = optionsPanels[i].getChildPanelCount();
for (int j=0; j<childCount; j++) {
addOptionPanel(optionsPanels[i].getChildPanel(j));
}
}
// Must be done after currentOptionPanelLayout is created.
// Must check option panel count because of 1-param constructor.
if (numOptionPanels>0)
optionTree.setSelectionRow(0);
// Must do this so child nodes of (hidden) root are visible.
optionTree.expandPath(new TreePath(root));
pack();
}
/*****************************************************************************/
/**
* Sets the visible option panel to the one specified.
*
* @param panel The index of the panel to display. If less than
* <code>-1</code> then the first visible panel is dispalyed; if
* greater than the number of options panels then the last option
* panel is displayed.
*/
public void setVisibleOptionPanel(int panel) {
// Check to see if a text field was edited in the previous
// panel (as if they switch Options panels before the text field
// loses focus, the Apply button won't get enabled).
int currentPanel = optionTree.getRowForPath(
optionTree.getSelectionPath());
if (optionsPanels[currentPanel].hasUnsavedChanges())
setApplyButtonEnabled(true);
// Ensure a valid panel number.
if (panel<0)
panel = 0;
else if (panel>optionsPanels.length)
panel = optionsPanels.length-1;
// Show the next panel.
optionTree.setSelectionRow(panel);
}
/*****************************************************************************/
/**
* This method is overridden to ensure that all nodes in the tree are
* expanded (as if they're not, the size of the window and its widgets
* will be incorrect; not enough room for the tree).
*
* @param visible Whether or not this dialog should be visible.
*/
public void setVisible(boolean visible) {
if (visible) {
UIUtilities.expandAllNodes(optionTree);
// Set the preferred size of the scroll pane so that it stays the
// same size no matter what nodes they click on/hide. If we
// don't do this, the JScrollPane resizes when you click on nodes
// to show the entire displayed node - annoying!
Dimension size = optionTree.getPreferredScrollableViewportSize();
size.width += 45; // Just in case a scrollbar is there.
optionTreeScrollPane.setPreferredSize(size);
}
super.setVisible(visible);
}
/*****************************************************************************/
/**
* Listens for the user to select values in the tree.
*/
public void valueChanged(TreeSelectionEvent e) {
// Be careful and check for nulls as we support branch nodes not
// actually containing an option panel.
TreePath path = optionTree.getSelectionPath();
if (path==null)
return;
Object obj = path.getLastPathComponent();
if (obj==null)
return;
obj = ((DefaultMutableTreeNode)obj).getUserObject();
if (!(obj instanceof OptionsDialogPanel))
return;
OptionsDialogPanel panel = (OptionsDialogPanel)obj;
if (panel==null)
return;
currentOptionPanelLayout.show(currentOptionPanel, panel.getName());
// Give the "top" component on the panel focus, just to make
// things a little nicer.
JComponent topComponent = panel.getTopJComponent();
topComponent.requestFocusInWindow();
if (topComponent instanceof JTextComponent)
((JTextComponent)topComponent).selectAll();
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -