X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fnet%2Fsf%2Fopenrocket%2Fgui%2Fmain%2FComponentAddButtons.java;h=a5bdb789b9b9096102bb0159ec0ab6a2e8037889;hb=8e4a8571c20ba96164ddd8a68226d43ccd549d21;hp=8e67b6a599655d80c1fa0e75bb6cdfc2fbfb8f25;hpb=c72e1c03cc0d15e11368707c38721d506ce356b9;p=debian%2Fopenrocket diff --git a/src/net/sf/openrocket/gui/main/ComponentAddButtons.java b/src/net/sf/openrocket/gui/main/ComponentAddButtons.java index 8e67b6a5..a5bdb789 100644 --- a/src/net/sf/openrocket/gui/main/ComponentAddButtons.java +++ b/src/net/sf/openrocket/gui/main/ComponentAddButtons.java @@ -29,6 +29,9 @@ import net.miginfocom.swing.MigLayout; import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.gui.components.StyledLabel; import net.sf.openrocket.gui.configdialog.ComponentConfigDialog; +import net.sf.openrocket.gui.main.componenttree.ComponentTreeModel; +import net.sf.openrocket.l10n.Translator; +import net.sf.openrocket.logging.LogHelper; import net.sf.openrocket.rocketcomponent.BodyComponent; import net.sf.openrocket.rocketcomponent.BodyTube; import net.sf.openrocket.rocketcomponent.Bulkhead; @@ -48,6 +51,7 @@ import net.sf.openrocket.rocketcomponent.Streamer; import net.sf.openrocket.rocketcomponent.Transition; import net.sf.openrocket.rocketcomponent.TrapezoidFinSet; import net.sf.openrocket.rocketcomponent.TubeCoupler; +import net.sf.openrocket.startup.Application; import net.sf.openrocket.util.BugException; import net.sf.openrocket.util.Pair; import net.sf.openrocket.util.Prefs; @@ -62,11 +66,13 @@ import net.sf.openrocket.util.Reflection; */ public class ComponentAddButtons extends JPanel implements Scrollable { - + private static final LogHelper log = Application.getLogger(); + private static final Translator trans = Application.getTranslator(); + private static final int ROWS = 3; private static final int MAXCOLS = 6; private static final String BUTTONPARAM = "grow, sizegroup buttons"; - + private static final int GAP = 5; private static final int EXTRASPACE = 0; @@ -80,15 +86,15 @@ public class ComponentAddButtons extends JPanel implements Scrollable { private final int width, height; - public ComponentAddButtons(OpenRocketDocument document, TreeSelectionModel model, + public ComponentAddButtons(OpenRocketDocument document, TreeSelectionModel model, JViewport viewport) { super(); String constaint = "[min!]"; - for (int i=1; i w) w = d.width; @@ -150,12 +172,11 @@ public class ComponentAddButtons extends JPanel implements Scrollable { } // Set all buttons to maximum size - System.out.println("Setting w="+w+" h="+h); - width=w; - height=h; - Dimension d = new Dimension(width,height); - for (row=0; row < buttons.length; row++) { - for (int col=0; col < buttons[row].length; col++) { + width = w; + height = h; + Dimension d = new Dimension(width, height); + for (row = 0; row < buttons.length; row++) { + for (int col = 0; col < buttons[row].length; col++) { buttons[row][col].setMinimumSize(d); buttons[row][col].setPreferredSize(d); buttons[row][col].getComponent(0).validate(); @@ -166,6 +187,7 @@ public class ComponentAddButtons extends JPanel implements Scrollable { if (viewport != null) { viewport.addChangeListener(new ChangeListener() { private int oldWidth = -1; + public void stateChanged(ChangeEvent e) { Dimension d = ComponentAddButtons.this.viewport.getExtentSize(); if (d.width != oldWidth) { @@ -176,7 +198,7 @@ public class ComponentAddButtons extends JPanel implements Scrollable { }); } - add(new JPanel(),"grow"); + add(new JPanel(), "grow"); } @@ -186,50 +208,50 @@ public class ComponentAddButtons extends JPanel implements Scrollable { * @param row Row number * @param b List of ComponentButtons to place on the row */ - private void addButtonRow(String label, int row, ComponentButton ... b) { - if (row>0) - add(new JLabel(label),"span, gaptop unrel, wrap"); - else - add(new JLabel(label),"span, gaptop 0, wrap"); + private void addButtonRow(String label, int row, ComponentButton... b) { + if (row > 0) + add(new JLabel(label), "span, gaptop unrel, wrap"); + else + add(new JLabel(label), "span, gaptop 0, wrap"); - int col=0; + int col = 0; buttons[row] = new ComponentButton[b.length]; - - for (int i=0; i d.width) { + + for (int row = 0; row < buttons.length; row++) { + w = 0; + for (int col = 0; col < buttons[row].length; col++) { + w += GAP + width; + String param = BUTTONPARAM + ",width " + width + "!,height " + height + "!"; + + if (w + EXTRASPACE > d.width) { param = param + ",newline"; - w = GAP+width; + w = GAP + width; } - if (col == buttons[row].length-1) + if (col == buttons[row].length - 1) param = param + ",wrap"; layout.setComponentConstraints(buttons[row][col], param); } @@ -238,7 +260,7 @@ public class ComponentAddButtons extends JPanel implements Scrollable { } - + /** * Class for a component button. */ @@ -248,7 +270,7 @@ public class ComponentAddButtons extends JPanel implements Scrollable { /** Only label, no icon. */ public ComponentButton(String text) { - this(text,null,null); + this(text, null, null); } /** @@ -257,48 +279,48 @@ public class ComponentAddButtons extends JPanel implements Scrollable { */ public ComponentButton(String text, Icon enabled, Icon disabled) { super(); - setLayout(new MigLayout("fill, flowy, insets 0, gap 0","","")); + setLayout(new MigLayout("fill, flowy, insets 0, gap 0", "", "")); - add(new JLabel(),"push, sizegroup spacing"); + add(new JLabel(), "push, sizegroup spacing"); // Add Icon if (enabled != null) { JLabel label = new JLabel(enabled); if (disabled != null) label.setDisabledIcon(disabled); - add(label,"growx"); + add(label, "growx"); } - + // Add labels String[] l = text.split("\n"); - for (int i=0; i c, String text) { - this(text,ComponentIcons.getLargeIcon(c),ComponentIcons.getLargeDisabledIcon(c)); + this(text, ComponentIcons.getLargeIcon(c), ComponentIcons.getLargeDisabledIcon(c)); - if (c==null) + if (c == null) return; - componentClass = c; - + componentClass = c; + try { constructor = c.getConstructor(); } catch (NoSuchMethodException e) { - throw new IllegalArgumentException("Unable to get default "+ - "constructor for class "+c,e); + throw new IllegalArgumentException("Unable to get default " + + "constructor for class " + c, e); } } @@ -308,9 +330,9 @@ public class ComponentAddButtons extends JPanel implements Scrollable { * c is null if there is no selection. The default is to use c.isCompatible(class). */ public boolean isAddable(RocketComponent c) { - if (c==null) + if (c == null) return false; - if (componentClass==null) + if (componentClass == null) return false; return c.isCompatible(componentClass); } @@ -345,42 +367,44 @@ public class ComponentAddButtons extends JPanel implements Scrollable { public void setEnabled(boolean enabled) { super.setEnabled(enabled); Component[] c = getComponents(); - for (int i=0; i pos = getAdditionPosition(c); - if (pos==null) { + if (pos == null) { // Cancel addition + log.info("No position to add component"); return; } c = pos.getU(); position = pos.getV(); - + if (c == null) { // Should not occur ExceptionHandler.handleErrorCondition("ERROR: Could not place new component."); @@ -389,26 +413,26 @@ public class ComponentAddButtons extends JPanel implements Scrollable { } if (constructor == null) { - System.err.println("ERROR: Construction of type not supported yet."); + ExceptionHandler.handleErrorCondition("ERROR: Construction of type not supported yet."); return; } RocketComponent component; try { - component = (RocketComponent)constructor.newInstance(); + component = (RocketComponent) constructor.newInstance(); } catch (InstantiationException e) { - throw new BugException("Could not construct new instance of class "+ - constructor,e); + throw new BugException("Could not construct new instance of class " + constructor, e); } catch (IllegalAccessException e) { - throw new BugException("Could not construct new instance of class "+ - constructor,e); + throw new BugException("Could not construct new instance of class " + constructor, e); } catch (InvocationTargetException e) { - throw Reflection.handleInvocationTargetException(e); + throw Reflection.handleWrappedException(e); } // Next undo position is set by opening the configuration dialog document.addUndoPosition("Add " + component.getComponentName()); + log.info("Adding component " + component.getComponentName() + " to component " + c.getComponentName() + + " position=" + position); if (position == null) c.addChild(component); @@ -419,14 +443,13 @@ public class ComponentAddButtons extends JPanel implements Scrollable { selectionModel.setSelectionPath(ComponentTreeModel.makeTreePath(component)); JFrame parent = null; - for (Component comp = ComponentAddButtons.this; comp != null; - comp = comp.getParent()) { + for (Component comp = ComponentAddButtons.this; comp != null; comp = comp.getParent()) { if (comp instanceof JFrame) { parent = (JFrame) comp; break; } } - + ComponentConfigDialog.showDialog(parent, document, component); } } @@ -439,15 +462,15 @@ public class ComponentAddButtons extends JPanel implements Scrollable { public BodyComponentButton(Class c, String text) { super(c, text); } - + public BodyComponentButton(String text, Icon enabled, Icon disabled) { super(text, enabled, disabled); } - + public BodyComponentButton(String text) { super(text); } - + @Override public boolean isAddable(RocketComponent c) { if (super.isAddable(c)) @@ -462,28 +485,31 @@ public class ComponentAddButtons extends JPanel implements Scrollable { @Override public Pair getAdditionPosition(RocketComponent c) { - if (super.isAddable(c)) // Handled automatically + if (super.isAddable(c)) // Handled automatically return super.getAdditionPosition(c); - + if (c == null || c instanceof Rocket) { // Add as last body component of the last stage Rocket rocket = document.getRocket(); - return new Pair(rocket.getChild(rocket.getStageCount()-1), + return new Pair(rocket.getChild(rocket.getStageCount() - 1), null); } if (!(c instanceof BodyComponent)) return null; RocketComponent parent = c.getParent(); - assert(parent != null); + if (parent == null) { + throw new BugException("Component " + c.getComponentName() + " is the root component, " + + "componentClass=" + componentClass); + } // Check whether to insert between or at the end. // 0 = ask, 1 = in between, 2 = at the end int pos = Prefs.getChoise(Prefs.BODY_COMPONENT_INSERT_POSITION_KEY, 2, 0); - if (pos==0) { - if (parent.getChildPosition(c) == parent.getChildCount()-1) - pos = 2; // Selected component is the last component + if (pos == 0) { + if (parent.getChildPosition(c) == parent.getChildCount() - 1) + pos = 2; // Selected component is the last component else pos = askPosition(); } @@ -494,37 +520,44 @@ public class ComponentAddButtons extends JPanel implements Scrollable { return null; case 1: // Insert after current position - return new Pair(parent, parent.getChildPosition(c)+1); + return new Pair(parent, parent.getChildPosition(c) + 1); case 2: // Insert at the end of the parent - return new Pair(parent, null); + return new Pair(parent, null); default: - ExceptionHandler.handleErrorCondition("ERROR: Bad position type: "+pos); + ExceptionHandler.handleErrorCondition("ERROR: Bad position type: " + pos); return null; } } private int askPosition() { - Object[] options = { "Insert here", "Add to the end", "Cancel" }; + //// Insert here + //// Add to the end + //// Cancel + Object[] options = { trans.get("compaddbuttons.askPosition.Inserthere"), + trans.get("compaddbuttons.askPosition.Addtotheend"), + trans.get("compaddbuttons.askPosition.Cancel") }; JPanel panel = new JPanel(new MigLayout()); - JCheckBox check = new JCheckBox("Do not ask me again"); - panel.add(check,"wrap"); - panel.add(new StyledLabel("You can change the default operation in the " + - "preferences.",-2)); + //// Do not ask me again + JCheckBox check = new JCheckBox(trans.get("compaddbuttons.Donotaskmeagain")); + panel.add(check, "wrap"); + //// You can change the default operation in the preferences. + panel.add(new StyledLabel(trans.get("compaddbuttons.lbl.Youcanchange"), -2)); - int sel = JOptionPane.showOptionDialog(null, // parent component + int sel = JOptionPane.showOptionDialog(null, // parent component + //// Insert the component after the current component or as the last component? new Object[] { - "Insert the component after the current component or as the last " + - "component?", - panel }, - "Select component position", // title - JOptionPane.DEFAULT_OPTION, // default selections - JOptionPane.QUESTION_MESSAGE, // dialog type - null, // icon - options, // options - options[0]); // initial value - + trans.get("compaddbuttons.lbl.insertcomp"), + panel }, + //// Select component position + trans.get("compaddbuttons.Selectcomppos"), // title + JOptionPane.DEFAULT_OPTION, // default selections + JOptionPane.QUESTION_MESSAGE, // dialog type + null, // icon + options, // options + options[0]); // initial value + switch (sel) { case JOptionPane.CLOSED_OPTION: case 2: @@ -539,7 +572,7 @@ public class ComponentAddButtons extends JPanel implements Scrollable { sel = 2; break; default: - ExceptionHandler.handleErrorCondition("ERROR: JOptionPane returned "+sel); + ExceptionHandler.handleErrorCondition("ERROR: JOptionPane returned " + sel); return 0; } @@ -552,7 +585,7 @@ public class ComponentAddButtons extends JPanel implements Scrollable { } - + /** * Class for fin sets, that attach only to BodyTubes. @@ -561,33 +594,33 @@ public class ComponentAddButtons extends JPanel implements Scrollable { public FinButton(Class c, String text) { super(c, text); } - + public FinButton(String text, Icon enabled, Icon disabled) { super(text, enabled, disabled); } - + public FinButton(String text) { super(text); } - + @Override public boolean isAddable(RocketComponent c) { - if (c==null) + if (c == null) return false; return (c.getClass().equals(BodyTube.class)); } } - - - ///////// Scrolling functionality + + ///////// Scrolling functionality + @Override public Dimension getPreferredScrollableViewportSize() { return getPreferredSize(); } - - + + @Override public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { @@ -595,20 +628,20 @@ public class ComponentAddButtons extends JPanel implements Scrollable { return visibleRect.height * 8 / 10; return 10; } - - + + @Override public boolean getScrollableTracksViewportHeight() { return false; } - - + + @Override public boolean getScrollableTracksViewportWidth() { return true; } - - + + @Override public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { @@ -616,4 +649,3 @@ public class ComponentAddButtons extends JPanel implements Scrollable { } } -