1 19 20 package org.netbeans.modules.form.actions; 21 22 import java.awt.*; 23 import javax.swing.*; 24 import java.util.ResourceBundle ; 25 26 import org.openide.*; 27 import org.openide.awt.Mnemonics; 28 import org.openide.util.HelpCtx; 29 import org.openide.util.NbBundle; 30 import org.openide.util.actions.*; 31 import org.openide.nodes.*; 32 33 import org.netbeans.modules.form.*; 34 import org.netbeans.modules.form.layoutdesign.*; 35 36 41 public class CustomizeEmptySpaceAction extends CookieAction { 42 private static String name; 43 private Dialog dialog; 44 45 protected int mode() { 46 return MODE_EXACTLY_ONE; 47 } 48 49 protected Class [] cookieClasses() { 50 return new Class [] { RADComponentCookie.class }; 51 } 52 53 protected boolean asynchronous() { 54 return false; 55 } 56 57 62 public String getName() { 63 if (name == null) 64 name = org.openide.util.NbBundle.getBundle(CustomizeEmptySpaceAction.class) 65 .getString("ACT_CustomizeEmptySpace"); return name; 67 } 68 69 public HelpCtx getHelpCtx() { 70 return HelpCtx.DEFAULT_HELP; 71 } 72 73 protected String iconResource() { 74 return "org/openide/resources/actions/empty.gif"; } 76 77 82 protected void performAction(Node[] activatedNodes) { 83 java.util.List comps = FormUtils.getSelectedLayoutComponents(activatedNodes); 84 if ((comps == null) || (comps.size() != 1)) return; 85 RADComponent metacomp = (RADComponent)comps.get(0); 86 FormModel formModel = metacomp.getFormModel(); 87 LayoutModel model = formModel.getLayoutModel(); 88 final EmptySpaceCustomizer customizer = new EmptySpaceCustomizer(model, metacomp.getId()); 89 DialogDescriptor dd = new DialogDescriptor( 90 customizer, 91 NbBundle.getMessage(CustomizeEmptySpaceAction.class, "TITLE_CustomizeEmptySpace"), true, 93 NotifyDescriptor.OK_CANCEL_OPTION, 94 NotifyDescriptor.OK_OPTION, 95 DialogDescriptor.DEFAULT_ALIGN, 96 new HelpCtx(getClass().getName()), 97 new java.awt.event.ActionListener () { 98 public void actionPerformed(java.awt.event.ActionEvent evt) { 99 if (evt.getSource() == NotifyDescriptor.OK_OPTION) { 100 if (customizer.checkValues()) { 101 dialog.dispose(); 102 } 103 } 104 } 105 }); 106 dd.setClosingOptions(new Object [] {NotifyDescriptor.CANCEL_OPTION}); 107 dialog = DialogDisplayer.getDefault().createDialog(dd); 108 dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CustomizeEmptySpaceAction.class, "ACSD_EmptySpace")); dialog.setVisible(true); 110 dialog = null; 111 if (dd.getValue() == DialogDescriptor.OK_OPTION) { 112 Object layoutUndoMark = model.getChangeMark(); 113 javax.swing.undo.UndoableEdit ue = model.getUndoableEdit(); 114 boolean autoUndo = true; 115 try { 116 customizer.applyValues(); 117 autoUndo = false; 118 } finally { 119 formModel.fireContainerLayoutChanged(((RADVisualComponent)metacomp).getParentContainer(), null, null, null); 120 if (!layoutUndoMark.equals(model.getChangeMark())) { 121 formModel.addUndoableEdit(ue); 122 } 123 if (autoUndo) { 124 formModel.forceUndoOfCompoundEdit(); 125 } 126 } 127 } 128 } 129 130 protected boolean enable(Node[] activatedNodes) { 131 if (super.enable(activatedNodes)) { 132 java.util.List comps = FormUtils.getSelectedLayoutComponents(activatedNodes); 133 return ((comps != null) && (comps.size() == 1)); 134 } 135 return false; 136 } 137 138 private static class EmptySpaceCustomizer extends JPanel { 139 JComboBox leftSize = new JComboBox(); 140 JComboBox rightSize = new JComboBox(); 141 JComboBox topSize = new JComboBox(); 142 JComboBox bottomSize = new JComboBox(); 143 JCheckBox leftResizable = new JCheckBox(); 144 JCheckBox rightResizable = new JCheckBox(); 145 JCheckBox topResizable = new JCheckBox(); 146 JCheckBox bottomResizable = new JCheckBox(); 147 LayoutModel model; 148 String compId; 149 String padding; 150 151 EmptySpaceCustomizer(LayoutModel model, String compId) { 152 this.model = model; 153 this.compId = compId; 154 initComponents(); 155 LayoutComponent comp = model.getLayoutComponent(compId); 156 initValues(comp, LayoutConstants.HORIZONTAL, LayoutConstants.LEADING, leftSize, leftResizable); 157 initValues(comp, LayoutConstants.HORIZONTAL, LayoutConstants.TRAILING, rightSize, rightResizable); 158 initValues(comp, LayoutConstants.VERTICAL, LayoutConstants.LEADING, topSize, topResizable); 159 initValues(comp, LayoutConstants.VERTICAL, LayoutConstants.TRAILING, bottomSize, bottomResizable); 160 } 161 162 private void initValues(LayoutComponent comp, int dimension, int direction, JComboBox size, JCheckBox resizable) { 163 LayoutInterval space = LayoutUtils.getAdjacentEmptySpace(comp, dimension, direction); 164 if (space != null) { 165 int pref = space.getPreferredSize(false); 166 int max = space.getMaximumSize(false); 167 size.setSelectedItem((pref == LayoutConstants.NOT_EXPLICITLY_DEFINED) ? padding : ("" + pref)); 168 resizable.setSelected((max != LayoutConstants.USE_PREFERRED_SIZE) && (max != pref)); 169 } else { 170 size.setSelectedItem(NbBundle.getMessage(CustomizeEmptySpaceAction.class, "VALUE_NoEmptySpace")); 171 size.setEnabled(false); 172 resizable.setEnabled(false); 173 } 174 } 175 176 boolean checkValues() { 177 return checkValue(leftSize) && checkValue(rightSize) && checkValue(topSize) && checkValue(bottomSize); 178 } 179 180 private boolean checkValue(JComboBox size) { 181 Object selSize = size.getSelectedItem(); 182 if (size.isEnabled() && !selSize.equals(padding)) { 183 try { 184 int newPref = Integer.parseInt((String )selSize); 185 if (newPref < 0) { 186 notify("MSG_NegativeSpaceSize"); return false; 189 } 190 if (newPref > Short.MAX_VALUE) { 191 notify("MSG_TooLargeSpaceSize"); return false; 194 } 195 } catch (NumberFormatException nfex) { 196 notify("MSG_CorruptedSpaceSize"); return false; 199 } 200 } 201 return true; 202 } 203 204 private void notify(String messageKey) { 205 NotifyDescriptor descriptor = new NotifyDescriptor.Message( 206 NbBundle.getBundle(CustomizeEmptySpaceAction.class).getString(messageKey)); 207 DialogDisplayer.getDefault().notify(descriptor); 208 } 209 210 void applyValues() { 211 LayoutComponent comp = model.getLayoutComponent(compId); 212 applyValues(comp, LayoutConstants.HORIZONTAL, LayoutConstants.LEADING, leftSize, leftResizable); 213 applyValues(comp, LayoutConstants.HORIZONTAL, LayoutConstants.TRAILING, rightSize, rightResizable); 214 applyValues(comp, LayoutConstants.VERTICAL, LayoutConstants.LEADING, topSize, topResizable); 215 applyValues(comp, LayoutConstants.VERTICAL, LayoutConstants.TRAILING, bottomSize, bottomResizable); 216 } 217 218 private void applyValues(LayoutComponent comp, int dimension, int direction, JComboBox size, JCheckBox resizable) { 219 LayoutInterval space = LayoutUtils.getAdjacentEmptySpace(comp, dimension, direction); 220 if (space != null) { 221 int pref = space.getPreferredSize(false); 222 int max = space.getMaximumSize(false); 223 boolean oldResizable = (max != LayoutConstants.USE_PREFERRED_SIZE) && (max != pref); 224 boolean newResizable = resizable.isSelected(); 225 Object selSize = size.getSelectedItem(); 226 int newPref; 227 if (selSize.equals(padding)){ 228 newPref = LayoutConstants.NOT_EXPLICITLY_DEFINED; 229 } else { 230 try { 231 newPref = Integer.parseInt((String )selSize); 232 if (newPref < 0) { 233 newPref = pref; 234 } 235 } catch (NumberFormatException nfex) { 236 newPref = pref; } 238 } 239 if ((pref != newPref) || (oldResizable != newResizable)) { 240 model.setIntervalSize(space, 241 newResizable ? LayoutConstants.NOT_EXPLICITLY_DEFINED : LayoutConstants.USE_PREFERRED_SIZE, 242 newPref, 243 newResizable ? Short.MAX_VALUE : LayoutConstants.USE_PREFERRED_SIZE); 244 } 245 } 246 } 247 248 private void initComponents() { 249 ResourceBundle bundle = NbBundle.getBundle(EmptySpaceCustomizer.class); 250 setLayout(new GridBagLayout()); 251 setBorder(new javax.swing.border.TitledBorder (bundle.getString("TITLE_EmptySpace"))); JLabel leftLabel = new JLabel(); 253 JLabel rightLabel = new JLabel(); 254 JLabel topLabel = new JLabel(); 255 JLabel bottomLabel = new JLabel(); 256 JLabel sizeLabel = new JLabel(bundle.getString("NAME_SpaceSize")); JLabel resizableLabel = new JLabel(bundle.getString("NAME_SpaceResizable")); 259 GridBagConstraints gridBagConstraints = new GridBagConstraints(); 260 gridBagConstraints.gridx = 0; 261 gridBagConstraints.gridy = 1; 262 gridBagConstraints.anchor = GridBagConstraints.EAST; 263 gridBagConstraints.insets = new Insets(3, 6, 3, 0); 264 add(leftLabel, gridBagConstraints); 265 266 gridBagConstraints.gridy = 2; 267 add(rightLabel, gridBagConstraints); 268 269 gridBagConstraints.gridy = 3; 270 add(topLabel, gridBagConstraints); 271 272 gridBagConstraints.gridy = 4; 273 gridBagConstraints.insets = new Insets(3, 6, 6, 0); 274 add(bottomLabel, gridBagConstraints); 275 276 gridBagConstraints.gridx = 1; 277 gridBagConstraints.gridy = 0; 278 gridBagConstraints.anchor = GridBagConstraints.CENTER; 279 gridBagConstraints.insets = new Insets(0, 6, 3, 6); 280 add(sizeLabel, gridBagConstraints); 281 282 gridBagConstraints.gridx = 2; 283 gridBagConstraints.insets = new Insets(0, 6, 3, 6); 284 add(resizableLabel, gridBagConstraints); 285 286 gridBagConstraints.insets = new Insets(0, 0, 0, 0); 287 gridBagConstraints.gridx = 2; 288 gridBagConstraints.gridy = 1; 289 add(leftResizable, gridBagConstraints); 290 291 gridBagConstraints.gridy = 2; 292 add(rightResizable, gridBagConstraints); 293 294 gridBagConstraints.gridy = 3; 295 add(topResizable, gridBagConstraints); 296 297 gridBagConstraints.gridy = 4; 298 gridBagConstraints.insets = new Insets(0, 0, 3, 0); 299 add(bottomResizable, gridBagConstraints); 300 301 leftLabel.setLabelFor(leftSize); 302 rightLabel.setLabelFor(rightSize); 303 topLabel.setLabelFor(topSize); 304 bottomLabel.setLabelFor(bottomSize); 305 306 Mnemonics.setLocalizedText(leftLabel, bundle.getString("NAME_LeftSpace")); Mnemonics.setLocalizedText(rightLabel, bundle.getString("NAME_RightSpace")); Mnemonics.setLocalizedText(topLabel, bundle.getString("NAME_TopSpace")); Mnemonics.setLocalizedText(bottomLabel, bundle.getString("NAME_BottomSpace")); 311 leftSize.setEditable(true); 312 rightSize.setEditable(true); 313 topSize.setEditable(true); 314 bottomSize.setEditable(true); 315 316 padding = bundle.getString("VALUE_DefaultPadding"); leftSize.setModel(new DefaultComboBoxModel(new String [] {padding})); 318 rightSize.setModel(new DefaultComboBoxModel(new String [] {padding})); 319 topSize.setModel(new DefaultComboBoxModel(new String [] {padding})); 320 bottomSize.setModel(new DefaultComboBoxModel(new String [] {padding})); 321 322 leftResizable.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_LeftResizable")); rightResizable.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_RightResizable")); topResizable.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_TopResizable")); bottomResizable.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_BottomResizable")); 327 gridBagConstraints.gridx = 1; 328 gridBagConstraints.gridy = 1; 329 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 330 gridBagConstraints.weightx = 1.0; 331 gridBagConstraints.weighty = 1.0; 332 gridBagConstraints.insets = new Insets(3, 6, 3, 6); 333 add(leftSize, gridBagConstraints); 334 335 gridBagConstraints.gridy = 2; 336 add(rightSize, gridBagConstraints); 337 338 gridBagConstraints.gridy = 3; 339 add(topSize, gridBagConstraints); 340 341 gridBagConstraints.gridy = 4; 342 gridBagConstraints.insets = new Insets(3, 6, 6, 6); 343 add(bottomSize, gridBagConstraints); 344 } 345 346 } 347 } 348 | Popular Tags |