X-Git-Url: https://git.gag.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Fnet%2Fsf%2Fopenrocket%2Futil%2FGUIUtil.java;h=831f2381afc44b53f13d84fb536cfc38d87572dd;hb=8320c04afa30e2aa0150adc870d02abeedb01066;hp=4f552b819310b7ae7ced7f82518320612c5fec84;hpb=4e356b38bfc24fc25a2fcf0ddbf0dee3a72d5f41;p=debian%2Fopenrocket diff --git a/src/net/sf/openrocket/util/GUIUtil.java b/src/net/sf/openrocket/util/GUIUtil.java index 4f552b81..831f2381 100644 --- a/src/net/sf/openrocket/util/GUIUtil.java +++ b/src/net/sf/openrocket/util/GUIUtil.java @@ -2,6 +2,7 @@ package net.sf.openrocket.util; import java.awt.Component; import java.awt.Container; +import java.awt.Font; import java.awt.Image; import java.awt.KeyboardFocusManager; import java.awt.Point; @@ -21,16 +22,16 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; -import java.util.Enumeration; import java.util.HashSet; import java.util.List; import java.util.Set; -import java.util.Vector; import javax.imageio.ImageIO; import javax.swing.AbstractAction; import javax.swing.AbstractButton; import javax.swing.Action; +import javax.swing.BoundedRangeModel; +import javax.swing.ComboBoxModel; import javax.swing.DefaultBoundedRangeModel; import javax.swing.DefaultComboBoxModel; import javax.swing.DefaultListSelectionModel; @@ -44,19 +45,25 @@ import javax.swing.JSpinner; import javax.swing.JTable; import javax.swing.JTree; import javax.swing.KeyStroke; +import javax.swing.ListSelectionModel; import javax.swing.LookAndFeel; import javax.swing.RootPaneContainer; +import javax.swing.SpinnerModel; import javax.swing.SpinnerNumberModel; import javax.swing.SwingUtilities; import javax.swing.UIManager; +import javax.swing.border.TitledBorder; import javax.swing.event.ChangeListener; import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableColumnModel; import javax.swing.table.DefaultTableModel; +import javax.swing.table.TableColumnModel; import javax.swing.table.TableModel; +import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.DefaultTreeSelectionModel; -import javax.swing.tree.TreeNode; +import javax.swing.tree.TreeModel; +import javax.swing.tree.TreeSelectionModel; import net.sf.openrocket.gui.Resettable; import net.sf.openrocket.logging.LogHelper; @@ -96,8 +103,9 @@ public class GUIUtil { /** * Set suitable options for a single-use disposable dialog. This includes - * setting ESC to close the dialog and adding the appropriate window icons. - * If defaultButton is provided, it is set to the default button action. + * setting ESC to close the dialog, adding the appropriate window icons and + * setting the location based on the platform. If defaultButton is provided, + * it is set to the default button action. *

* The default button must be already attached to the dialog. * @@ -108,6 +116,9 @@ public class GUIUtil { installEscapeCloseOperation(dialog); setWindowIcons(dialog); addModelNullingListener(dialog); + dialog.setLocationByPlatform(true); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.pack(); if (defaultButton != null) { setDefaultButton(defaultButton); } @@ -123,7 +134,9 @@ public class GUIUtil { */ public static void installEscapeCloseOperation(final JDialog dialog) { Action dispatchClosing = new AbstractAction() { + @Override public void actionPerformed(ActionEvent event) { + log.user("Closing dialog " + dialog); dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING)); } }; @@ -187,7 +200,9 @@ public class GUIUtil { window.addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent e) { + log.debug("Clearing all models of window " + window); setNullModels(window); + MemoryManagement.collectable(window); } }); } @@ -238,6 +253,45 @@ public class GUIUtil { } + /** + * Changes the size of the font of the specified component by the given amount. + * + * @param component the component for which to change the font + * @param size the change in the font size + */ + public static void changeFontSize(JComponent component, float size) { + Font font = component.getFont(); + font = font.deriveFont(font.getSize2D() + size); + component.setFont(font); + } + + + /** + * Changes the style of the font of the specified border. + * + * @param border the component for which to change the font + * @param style the change in the font style + */ + public static void changeFontStyle(TitledBorder border, int style) { + /* + * There's been an NPE caused by the font changing, this is debug for it. + */ + if (border == null) { + throw new BugException("border is null"); + } + Font font = border.getTitleFont(); + if (font == null) { + throw new BugException("Border font is null"); + } + font = font.deriveFont(style); + if (font == null) { + throw new BugException("Derived font is null"); + } + border.setTitleFont(font); + } + + + /** * Traverses recursively the component tree, and sets all applicable component * models to null, so as to remove the listener connections. After calling this @@ -281,7 +335,11 @@ public class GUIUtil { for (ChangeListener l : spinner.getChangeListeners()) { spinner.removeChangeListener(l); } + SpinnerModel model = spinner.getModel(); spinner.setModel(new SpinnerNumberModel()); + if (model instanceof Invalidatable) { + ((Invalidatable) model).invalidate(); + } } else if (c instanceof JSlider) { @@ -289,7 +347,11 @@ public class GUIUtil { for (ChangeListener l : slider.getChangeListeners()) { slider.removeChangeListener(l); } + BoundedRangeModel model = slider.getModel(); slider.setModel(new DefaultBoundedRangeModel()); + if (model instanceof Invalidatable) { + ((Invalidatable) model).invalidate(); + } } else if (c instanceof JComboBox) { @@ -297,7 +359,11 @@ public class GUIUtil { for (ActionListener l : combo.getActionListeners()) { combo.removeActionListener(l); } + ComboBoxModel model = combo.getModel(); combo.setModel(new DefaultComboBoxModel()); + if (model instanceof Invalidatable) { + ((Invalidatable) model).invalidate(); + } } else if (c instanceof AbstractButton) { @@ -305,60 +371,51 @@ public class GUIUtil { for (ActionListener l : button.getActionListeners()) { button.removeActionListener(l); } + Action model = button.getAction(); button.setAction(new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { } }); + if (model instanceof Invalidatable) { + ((Invalidatable) model).invalidate(); + } } else if (c instanceof JTable) { JTable table = (JTable) c; + TableModel model1 = table.getModel(); table.setModel(new DefaultTableModel()); + if (model1 instanceof Invalidatable) { + ((Invalidatable) model1).invalidate(); + } + + TableColumnModel model2 = table.getColumnModel(); table.setColumnModel(new DefaultTableColumnModel()); + if (model2 instanceof Invalidatable) { + ((Invalidatable) model2).invalidate(); + } + + ListSelectionModel model3 = table.getSelectionModel(); table.setSelectionModel(new DefaultListSelectionModel()); + if (model3 instanceof Invalidatable) { + ((Invalidatable) model3).invalidate(); + } } else if (c instanceof JTree) { JTree tree = (JTree) c; - tree.setModel(new DefaultTreeModel(new TreeNode() { - @SuppressWarnings("unchecked") - @Override - public Enumeration children() { - return new Vector().elements(); - } - - @Override - public boolean getAllowsChildren() { - return false; - } - - @Override - public TreeNode getChildAt(int childIndex) { - return null; - } - - @Override - public int getChildCount() { - return 0; - } - - @Override - public int getIndex(TreeNode node) { - return 0; - } - - @Override - public TreeNode getParent() { - return null; - } - - @Override - public boolean isLeaf() { - return true; - } - })); + TreeModel model1 = tree.getModel(); + tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode())); + if (model1 instanceof Invalidatable) { + ((Invalidatable) model1).invalidate(); + } + + TreeSelectionModel model2 = tree.getSelectionModel(); tree.setSelectionModel(new DefaultTreeSelectionModel()); + if (model2 instanceof Invalidatable) { + ((Invalidatable) model2).invalidate(); + } } else if (c instanceof Resettable) { @@ -377,7 +434,6 @@ public class GUIUtil { - /** * A mouse listener that toggles the state of a boolean value in a table model * when clicked on another column of the table.