1 19 20 package org.netbeans.modules.apisupport.project.ui.customizer; 21 22 import java.awt.AlphaComposite ; 23 import java.awt.Graphics ; 24 import java.awt.Graphics2D ; 25 import java.io.File ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import javax.swing.ImageIcon ; 29 import javax.swing.JFileChooser ; 30 import javax.swing.JLabel ; 31 import javax.swing.event.DocumentEvent ; 32 import javax.swing.event.DocumentListener ; 33 import org.netbeans.modules.apisupport.project.ui.UIUtil; 34 import org.netbeans.spi.project.ui.support.ProjectCustomizer; 35 import org.openide.ErrorManager; 36 import org.openide.util.NbBundle; 37 38 43 final class SuiteCustomizerBasicBranding extends NbPropertyPanel.Suite { 44 45 private URL iconSource; 46 private ProjectCustomizer.Category cat; 47 private BasicCustomizer.SubCategoryProvider prov; 48 51 public SuiteCustomizerBasicBranding(final SuiteProperties suiteProps, ProjectCustomizer.Category cat, 52 BasicCustomizer.SubCategoryProvider prov) { 53 super(suiteProps, SuiteCustomizerBasicBranding.class); 54 initComponents(); 55 this.cat = cat; 56 this.prov = prov; 57 refresh(); 58 checkValidity(); 59 DocumentListener textFieldChangeListener = new UIUtil.DocumentAdapter() { 60 public void insertUpdate(DocumentEvent e) { 61 checkValidity(); 62 } 63 }; 64 nameValue.getDocument().addDocumentListener(textFieldChangeListener); 65 titleValue.getDocument().addDocumentListener(textFieldChangeListener); 66 } 67 68 public void addNotify() { 69 super.addNotify(); 70 if (prov != null) { 71 showSubCategory(prov); 72 prov = null; 74 } 75 } 76 77 78 protected void checkValidity() { 79 boolean panelValid = true; 80 81 if (panelValid && nameValue.getText().trim().length() == 0) { 82 cat.setErrorMessage(NbBundle.getMessage(SuiteCustomizerBasicBranding.class, "ERR_EmptyName")); panelValid = false; 84 } 85 86 if (panelValid && !nameValue.getText().trim().matches("[a-z][a-z0-9]*(_[a-z][a-z0-9]*)*")) { cat.setErrorMessage(NbBundle.getMessage(SuiteCustomizerBasicBranding.class, "ERR_InvalidName")); panelValid = false; 89 } 90 91 if (panelValid && titleValue.getText().trim().length() == 0) { 92 cat.setErrorMessage(NbBundle.getMessage(SuiteCustomizerBasicBranding.class, "ERR_EmptyTitle")); panelValid = false; 94 } 95 96 if (panelValid) { 97 cat.setErrorMessage(null); 98 } 99 cat.setValid(panelValid); 100 } 101 102 void refresh() { 103 getBrandingModel().brandingEnabledRefresh(); 104 getBrandingModel().initName(true); 105 getBrandingModel().initTitle(true); 106 standaloneApp.setSelected(getBrandingModel().isBrandingEnabled()); 107 addOn.setSelected(!getBrandingModel().isBrandingEnabled()); 108 nameValue.setText(getBrandingModel().getName()); 109 titleValue.setText(getBrandingModel().getTitle()); 110 iconSource = getBrandingModel().getIconSource(); 111 if (iconSource != null) { 112 ((ImagePreview)iconPreview).setImage(new ImageIcon (iconSource)); 113 } 114 116 enableOrDisableComponents(); 117 118 } 119 120 public void store() { 121 getBrandingModel().setName(nameValue.getText()); 123 getBrandingModel().setTitle(titleValue.getText()); 124 getBrandingModel().setIconSource(iconSource); 125 } 126 127 private void enableOrDisableComponents() { 128 nameValue.setEnabled(standaloneApp.isSelected()); 129 name.setEnabled(standaloneApp.isSelected()); 130 131 titleValue.setEnabled(standaloneApp.isSelected()); 132 title.setEnabled(standaloneApp.isSelected()); 133 134 browse.setEnabled(standaloneApp.isSelected()); 135 136 icon.setEnabled(standaloneApp.isSelected()); 137 138 iconPreview.setEnabled(standaloneApp.isSelected()); 139 } 140 141 146 private void initComponents() { 148 java.awt.GridBagConstraints gridBagConstraints; 149 150 buttonGroup1 = new javax.swing.ButtonGroup (); 151 title = new javax.swing.JLabel (); 152 titleValue = new javax.swing.JTextField (); 153 iconPreview = new ImagePreview(BasicBrandingModel.ICON_WIDTH, BasicBrandingModel.ICON_HEIGHT); 154 name = new javax.swing.JLabel (); 155 nameValue = new javax.swing.JTextField (); 156 browse = new javax.swing.JButton (); 157 icon = new javax.swing.JLabel (); 158 addOn = new javax.swing.JRadioButton (); 159 standaloneApp = new javax.swing.JRadioButton (); 160 161 setLayout(new java.awt.GridBagLayout ()); 162 163 title.setLabelFor(titleValue); 164 org.openide.awt.Mnemonics.setLocalizedText(title, java.util.ResourceBundle.getBundle("org/netbeans/modules/apisupport/project/ui/customizer/Bundle").getString("LBL_AppTitle")); 165 gridBagConstraints = new java.awt.GridBagConstraints (); 166 gridBagConstraints.gridx = 0; 167 gridBagConstraints.gridy = 3; 168 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 169 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 170 gridBagConstraints.insets = new java.awt.Insets (6, 0, 0, 0); 171 add(title, gridBagConstraints); 172 title.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/apisupport/project/ui/customizer/Bundle").getString("ACS_Title")); 173 174 gridBagConstraints = new java.awt.GridBagConstraints (); 175 gridBagConstraints.gridx = 1; 176 gridBagConstraints.gridy = 3; 177 gridBagConstraints.gridwidth = 2; 178 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 179 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 180 gridBagConstraints.weightx = 1.0; 181 gridBagConstraints.insets = new java.awt.Insets (6, 0, 0, 0); 182 add(titleValue, gridBagConstraints); 183 184 iconPreview.setLabelFor(iconPreview); 185 gridBagConstraints = new java.awt.GridBagConstraints (); 186 gridBagConstraints.gridx = 1; 187 gridBagConstraints.gridy = 4; 188 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 189 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 190 gridBagConstraints.weightx = 1.0; 191 gridBagConstraints.weighty = 1.0; 192 gridBagConstraints.insets = new java.awt.Insets (23, 0, 0, 12); 193 add(iconPreview, gridBagConstraints); 194 195 name.setLabelFor(nameValue); 196 org.openide.awt.Mnemonics.setLocalizedText(name, java.util.ResourceBundle.getBundle("org/netbeans/modules/apisupport/project/ui/customizer/Bundle").getString("LBL_AppName")); 197 gridBagConstraints = new java.awt.GridBagConstraints (); 198 gridBagConstraints.gridx = 0; 199 gridBagConstraints.gridy = 2; 200 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 201 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 202 gridBagConstraints.insets = new java.awt.Insets (18, 0, 0, 0); 203 add(name, gridBagConstraints); 204 name.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/apisupport/project/ui/customizer/Bundle").getString("ACS_Name")); 205 206 gridBagConstraints = new java.awt.GridBagConstraints (); 207 gridBagConstraints.gridx = 1; 208 gridBagConstraints.gridy = 2; 209 gridBagConstraints.gridwidth = 2; 210 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 211 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 212 gridBagConstraints.weightx = 1.0; 213 gridBagConstraints.insets = new java.awt.Insets (18, 0, 0, 0); 214 add(nameValue, gridBagConstraints); 215 216 org.openide.awt.Mnemonics.setLocalizedText(browse, java.util.ResourceBundle.getBundle("org/netbeans/modules/apisupport/project/ui/customizer/Bundle").getString("CTL_Browse")); 217 browse.addActionListener(new java.awt.event.ActionListener () { 218 public void actionPerformed(java.awt.event.ActionEvent evt) { 219 browseActionPerformed(evt); 220 } 221 }); 222 223 gridBagConstraints = new java.awt.GridBagConstraints (); 224 gridBagConstraints.gridx = 2; 225 gridBagConstraints.gridy = 4; 226 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST; 227 gridBagConstraints.insets = new java.awt.Insets (23, 0, 0, 0); 228 add(browse, gridBagConstraints); 229 browse.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/apisupport/project/ui/customizer/Bundle").getString("ACS_Browse")); 230 231 org.openide.awt.Mnemonics.setLocalizedText(icon, java.util.ResourceBundle.getBundle("org/netbeans/modules/apisupport/project/ui/customizer/Bundle").getString("LBL_AppIcon")); 232 gridBagConstraints = new java.awt.GridBagConstraints (); 233 gridBagConstraints.gridx = 0; 234 gridBagConstraints.gridy = 4; 235 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 236 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 237 gridBagConstraints.insets = new java.awt.Insets (23, 0, 0, 12); 238 add(icon, gridBagConstraints); 239 240 buttonGroup1.add(addOn); 241 org.openide.awt.Mnemonics.setLocalizedText(addOn, java.util.ResourceBundle.getBundle("org/netbeans/modules/apisupport/project/ui/customizer/Bundle").getString("LBL_AppAddOn")); 242 addOn.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 243 addOn.setMargin(new java.awt.Insets (0, 0, 0, 0)); 244 addOn.addActionListener(new java.awt.event.ActionListener () { 245 public void actionPerformed(java.awt.event.ActionEvent evt) { 246 addOnActionPerformed(evt); 247 } 248 }); 249 250 gridBagConstraints = new java.awt.GridBagConstraints (); 251 gridBagConstraints.gridwidth = 3; 252 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 253 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 254 add(addOn, gridBagConstraints); 255 addOn.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/apisupport/project/ui/customizer/Bundle").getString("ACS_AddOn")); 256 257 buttonGroup1.add(standaloneApp); 258 org.openide.awt.Mnemonics.setLocalizedText(standaloneApp, java.util.ResourceBundle.getBundle("org/netbeans/modules/apisupport/project/ui/customizer/Bundle").getString("LBL_AppStandAlone")); 259 standaloneApp.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 260 standaloneApp.setMargin(new java.awt.Insets (0, 0, 0, 0)); 261 standaloneApp.addActionListener(new java.awt.event.ActionListener () { 262 public void actionPerformed(java.awt.event.ActionEvent evt) { 263 standaloneAppActionPerformed(evt); 264 } 265 }); 266 267 gridBagConstraints = new java.awt.GridBagConstraints (); 268 gridBagConstraints.gridx = 0; 269 gridBagConstraints.gridy = 1; 270 gridBagConstraints.gridwidth = 3; 271 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 272 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 273 gridBagConstraints.insets = new java.awt.Insets (6, 0, 0, 0); 274 add(standaloneApp, gridBagConstraints); 275 standaloneApp.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/apisupport/project/ui/customizer/Bundle").getString("ACS_StandAloneApp")); 276 277 } 278 280 private void standaloneAppActionPerformed(java.awt.event.ActionEvent evt) { enableOrDisableComponents(); 283 getBrandingModel().setBrandingEnabled(standaloneApp.isSelected()); 284 } 286 private void addOnActionPerformed(java.awt.event.ActionEvent evt) { enableOrDisableComponents(); 288 getBrandingModel().setBrandingEnabled(standaloneApp.isSelected()); 289 } 291 private void browseActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = UIUtil.getIconFileChooser(); 293 int ret = chooser.showDialog(this, NbBundle.getMessage(getClass(), "LBL_Select")); if (ret == JFileChooser.APPROVE_OPTION) { 295 File file = chooser.getSelectedFile(); 296 try { 297 iconSource = file.toURI().toURL(); 298 } catch (MalformedURLException ex) { 299 ErrorManager.getDefault().notify(ex); 300 } 301 ((ImagePreview)iconPreview).setImage(new ImageIcon (iconSource)); 302 } 303 } 305 306 private javax.swing.JRadioButton addOn; 308 private javax.swing.JButton browse; 309 private javax.swing.ButtonGroup buttonGroup1; 310 private javax.swing.JLabel icon; 311 private javax.swing.JLabel iconPreview; 312 private javax.swing.JLabel name; 313 private javax.swing.JTextField nameValue; 314 private javax.swing.JRadioButton standaloneApp; 315 private javax.swing.JLabel title; 316 private javax.swing.JTextField titleValue; 317 319 static class ImagePreview extends JLabel { 320 private ImageIcon image = null; 321 private int width; 322 private int height; 323 ImagePreview(int width, int height){ 325 this.width = width; 327 this.height = height; 328 } 331 332 public void paint(Graphics g) { 333 super.paint(g); 334 Graphics2D g2d = (Graphics2D )g; 335 336 if (!isEnabled()) { 337 g2d.setComposite(AlphaComposite.getInstance( 338 AlphaComposite.SRC_OVER, 0.3f)); 339 } 340 341 if ((getWidth() > width) && (getHeight() > height) && image != null) { 342 345 int x = 0; int y = 0; g.drawImage(image.getImage(),x, y, width, height, this.getBackground(),null); 348 } 353 } 354 355 private void setImage(ImageIcon image) { 356 this.image = image; 357 repaint(); 358 } 359 } 360 361 private BasicBrandingModel getBrandingModel() { 362 return getProperties().getBrandingModel(); 363 } 364 365 private void showSubCategory(BasicCustomizer.SubCategoryProvider prov) { 366 if (SuiteCustomizer.APPLICATION.equals(prov.getCategory()) && 367 SuiteCustomizer.APPLICATION_CREATE_STANDALONE_APPLICATION.equals(prov.getSubcategory())) { 368 standaloneApp.requestFocus(); 369 } 370 } 371 372 } 373 | Popular Tags |