001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.java.ui;
042:
043: import java.awt.BorderLayout;
044: import java.awt.Rectangle;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047: import java.beans.PropertyChangeEvent;
048: import java.beans.PropertyChangeListener;
049: import java.util.ArrayList;
050: import java.util.HashMap;
051: import java.util.List;
052: import java.util.Map;
053: import java.util.prefs.AbstractPreferences;
054: import java.util.prefs.BackingStoreException;
055: import java.util.prefs.Preferences;
056: import javax.swing.DefaultComboBoxModel;
057: import javax.swing.JEditorPane;
058: import javax.swing.JPanel;
059: import javax.swing.UIManager;
060: import org.netbeans.spi.options.OptionsPanelController;
061: import org.openide.util.NbBundle;
062:
063: /**
064: *
065: * @author phrebejk
066: */
067: public class FormatingOptionsPanel extends JPanel implements
068: ActionListener, PropertyChangeListener {
069:
070: private FormatingOptionsPanelController fopControler;
071: private List<FormatingOptionsPanel.Category> categories = new ArrayList<FormatingOptionsPanel.Category>();
072:
073: private boolean loaded = false;
074:
075: /** Creates new form FormatingOptionsPanel */
076: public FormatingOptionsPanel(
077: FormatingOptionsPanelController fopControler) {
078: this .fopControler = fopControler;
079:
080: initComponents();
081:
082: if ("Windows".equals(UIManager.getLookAndFeel().getID())) //NOI18N
083: setOpaque(false);
084:
085: previewPane.setContentType("text/x-java"); // NOI18N
086: // Don't highlight caret row
087: previewPane
088: .putClientProperty(
089: "HighlightsLayerExcludes", // NOI18N
090: "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$" // NOI18N
091: );
092: previewPane.setText("1234567890123456789012345678901234567890"); // NOI18N
093: previewPane.setDoubleBuffered(true);
094: createCategories();
095:
096: DefaultComboBoxModel model = new DefaultComboBoxModel();
097: for (Category category : categories) {
098: model.addElement(category);
099: }
100: categoryCombo.setModel(model);
101:
102: categoryCombo.addActionListener(this );
103: actionPerformed(new ActionEvent(model, 0, null));
104: }
105:
106: void load() {
107: loaded = false;
108: for (Category category : categories) {
109: category.update();
110: }
111: loaded = true;
112: repaintPreview();
113: }
114:
115: void store() {
116: for (Category category : categories) {
117: category.applyChanges();
118: }
119: FmtOptions.flush();
120: FmtOptions.lastValues = null;
121: }
122:
123: void cancel() {
124: FmtOptions.lastValues = null;
125: }
126:
127: /** This method is called from within the constructor to
128: * initialize the form.
129: * WARNING: Do NOT modify this code. The content of this method is
130: * always regenerated by the Form Editor.
131: */
132: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
133: private void initComponents() {
134: java.awt.GridBagConstraints gridBagConstraints;
135:
136: jSplitPane1 = new javax.swing.JSplitPane();
137: optionsPanel = new javax.swing.JPanel();
138: categoryLabel = new javax.swing.JLabel();
139: categoryCombo = new javax.swing.JComboBox();
140: categoryPanel = new javax.swing.JPanel();
141: previewPanel = new javax.swing.JPanel();
142: jScrollPane1 = new javax.swing.JScrollPane();
143: previewPane = new javax.swing.JEditorPane();
144:
145: setLayout(new java.awt.GridBagLayout());
146:
147: jSplitPane1.setBorder(null);
148: jSplitPane1.setDividerLocation(400);
149: jSplitPane1.setOpaque(false);
150:
151: optionsPanel.setBorder(javax.swing.BorderFactory
152: .createEmptyBorder(0, 0, 0, 8));
153: optionsPanel.setOpaque(false);
154: optionsPanel.setLayout(new java.awt.GridBagLayout());
155:
156: categoryLabel.setLabelFor(categoryCombo);
157: org.openide.awt.Mnemonics.setLocalizedText(categoryLabel,
158: org.openide.util.NbBundle.getMessage(
159: FormatingOptionsPanel.class, "LBL_Category")); // NOI18N
160: gridBagConstraints = new java.awt.GridBagConstraints();
161: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
162: optionsPanel.add(categoryLabel, gridBagConstraints);
163:
164: categoryCombo
165: .setModel(new javax.swing.DefaultComboBoxModel(
166: new String[] { "Item 1", "Item 2", "Item 3",
167: "Item 4" }));
168: gridBagConstraints = new java.awt.GridBagConstraints();
169: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
170: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
171: gridBagConstraints.weightx = 1.0;
172: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
173: optionsPanel.add(categoryCombo, gridBagConstraints);
174:
175: categoryPanel.setOpaque(false);
176: categoryPanel.setLayout(new java.awt.BorderLayout());
177: gridBagConstraints = new java.awt.GridBagConstraints();
178: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
179: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
180: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
181: gridBagConstraints.weightx = 1.0;
182: gridBagConstraints.weighty = 1.0;
183: optionsPanel.add(categoryPanel, gridBagConstraints);
184:
185: jSplitPane1.setLeftComponent(optionsPanel);
186:
187: previewPanel.setBorder(javax.swing.BorderFactory
188: .createEmptyBorder(0, 8, 0, 0));
189: previewPanel.setOpaque(false);
190: previewPanel.setLayout(new java.awt.GridBagLayout());
191:
192: jScrollPane1.setDoubleBuffered(true);
193:
194: previewPane.setEditable(false);
195: jScrollPane1.setViewportView(previewPane);
196:
197: gridBagConstraints = new java.awt.GridBagConstraints();
198: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
199: gridBagConstraints.weightx = 1.0;
200: gridBagConstraints.weighty = 1.0;
201: previewPanel.add(jScrollPane1, gridBagConstraints);
202:
203: jSplitPane1.setRightComponent(previewPanel);
204:
205: gridBagConstraints = new java.awt.GridBagConstraints();
206: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
207: gridBagConstraints.weightx = 1.0;
208: gridBagConstraints.weighty = 1.0;
209: gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
210: add(jSplitPane1, gridBagConstraints);
211: }// </editor-fold>//GEN-END:initComponents
212:
213: // Variables declaration - do not modify//GEN-BEGIN:variables
214: private javax.swing.JComboBox categoryCombo;
215: private javax.swing.JLabel categoryLabel;
216: private javax.swing.JPanel categoryPanel;
217: private javax.swing.JScrollPane jScrollPane1;
218: private javax.swing.JSplitPane jSplitPane1;
219: private javax.swing.JPanel optionsPanel;
220: private javax.swing.JEditorPane previewPane;
221: private javax.swing.JPanel previewPanel;
222:
223: // End of variables declaration//GEN-END:variables
224:
225: private void createCategories() {
226: categories.add(FmtTabsIndents.getController());
227: // categories.add(FmtCodeGeneration.getController());
228: categories.add(FmtAlignmentBraces.getController());
229: categories.add(FmtWrapping.getController());
230: categories.add(FmtBlankLines.getController());
231: categories.add(FmtSpaces.getController());
232: // categories.add(FmtImports.getController()); // XXX
233:
234: for (Category category : categories) {
235: category.addPropertyChangeListener(this );
236: }
237:
238: }
239:
240: // Change in the combo
241: public void actionPerformed(ActionEvent e) {
242: Category category = (Category) categoryCombo.getSelectedItem();
243: categoryPanel.setVisible(false);
244: categoryPanel.removeAll();
245: categoryPanel.add(category.getComponent(null),
246: BorderLayout.CENTER);
247: categoryPanel.setVisible(true);
248: if (loaded) {
249: repaintPreview();
250: }
251: }
252:
253: public static abstract class Category extends
254: OptionsPanelController {
255:
256: private String displayName;
257:
258: public Category(String displayNameKey) {
259: this .displayName = NbBundle.getMessage(
260: FormatingOptionsPanel.class, displayNameKey);
261: }
262:
263: public abstract void storeTo(Preferences preferences);
264:
265: public abstract void refreshPreview(JEditorPane pane,
266: Preferences p);
267:
268: @Override
269: public String toString() {
270: return displayName;
271: }
272:
273: }
274:
275: // Change in some of the subpanels
276: public void propertyChange(PropertyChangeEvent evt) {
277:
278: if (!loaded) {
279: return;
280: }
281:
282: // Notify the main controler that the page has changed
283: fopControler.changed();
284:
285: // Repaint the preview
286: repaintPreview();
287: }
288:
289: // XXX Only temporary
290:
291: private void repaintPreview() {
292:
293: Preferences p = new PreviewPreferences();
294:
295: for (Category category : categories) {
296: category.storeTo(p);
297: }
298:
299: Category category = (Category) categoryCombo.getSelectedItem();
300: jScrollPane1.setIgnoreRepaint(true);
301: category.refreshPreview(previewPane, p);
302: previewPane.setIgnoreRepaint(false);
303: previewPane.scrollRectToVisible(new Rectangle(0, 0, 10, 10));
304: previewPane.repaint(100);
305:
306: FmtOptions.lastValues = p;
307:
308: }
309:
310: public static class PreviewPreferences extends AbstractPreferences {
311:
312: private Map<String, Object> map = new HashMap<String, Object>();
313:
314: public PreviewPreferences() {
315: super (null, ""); // NOI18N
316: }
317:
318: protected void putSpi(String key, String value) {
319: map.put(key, value);
320: }
321:
322: protected String getSpi(String key) {
323: return (String) map.get(key);
324: }
325:
326: protected void removeSpi(String key) {
327: map.remove(key);
328: }
329:
330: protected void removeNodeSpi() throws BackingStoreException {
331: throw new UnsupportedOperationException(
332: "Not supported yet.");
333: }
334:
335: protected String[] keysSpi() throws BackingStoreException {
336: String array[] = new String[map.keySet().size()];
337: return map.keySet().toArray(array);
338: }
339:
340: protected String[] childrenNamesSpi()
341: throws BackingStoreException {
342: throw new UnsupportedOperationException(
343: "Not supported yet.");
344: }
345:
346: protected AbstractPreferences childSpi(String name) {
347: throw new UnsupportedOperationException(
348: "Not supported yet.");
349: }
350:
351: protected void syncSpi() throws BackingStoreException {
352: throw new UnsupportedOperationException(
353: "Not supported yet.");
354: }
355:
356: protected void flushSpi() throws BackingStoreException {
357: throw new UnsupportedOperationException(
358: "Not supported yet.");
359: }
360:
361: }
362:
363: }
|