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.ruby.options;
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.api.ruby.platform.RubyInstallation;
061: import org.netbeans.spi.options.OptionsPanelController;
062: import org.openide.util.NbBundle;
063:
064: /**
065: *
066: * @author phrebejk
067: */
068: public class FormatingOptionsPanel extends JPanel implements
069: ActionListener, PropertyChangeListener {
070:
071: private FormatingOptionsPanelController fopControler;
072: private List<FormatingOptionsPanel.Category> categories = new ArrayList<FormatingOptionsPanel.Category>();
073:
074: private boolean loaded = false;
075:
076: /** Creates new form FormatingOptionsPanel */
077: public FormatingOptionsPanel(
078: FormatingOptionsPanelController fopControler) {
079: this .fopControler = fopControler;
080:
081: initComponents();
082:
083: if ("Windows".equals(UIManager.getLookAndFeel().getID())) //NOI18N
084: setOpaque(false);
085:
086: previewPane.setContentType(RubyInstallation.RUBY_MIME_TYPE); // NOI18N
087: // Don't highlight caret row
088: previewPane
089: .putClientProperty(
090: "HighlightsLayerExcludes", // NOI18N
091: "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$" // NOI18N
092: );
093: previewPane.setText("1234567890123456789012345678901234567890"); // NOI18N
094: previewPane.setDoubleBuffered(true);
095: createCategories();
096:
097: DefaultComboBoxModel model = new DefaultComboBoxModel();
098: for (Category category : categories) {
099: model.addElement(category);
100: }
101: categoryCombo.setModel(model);
102:
103: categoryCombo.addActionListener(this );
104: actionPerformed(new ActionEvent(model, 0, null));
105: }
106:
107: void load() {
108: loaded = false;
109: for (Category category : categories) {
110: category.update();
111: }
112: loaded = true;
113: repaintPreview();
114: }
115:
116: void store() {
117: for (Category category : categories) {
118: category.applyChanges();
119: }
120: FmtOptions.flush();
121: FmtOptions.lastValues = null;
122: }
123:
124: void cancel() {
125: FmtOptions.lastValues = null;
126: }
127:
128: /** This method is called from within the constructor to
129: * initialize the form.
130: * WARNING: Do NOT modify this code. The content of this method is
131: * always regenerated by the Form Editor.
132: */
133: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
134: private void initComponents() {
135: java.awt.GridBagConstraints gridBagConstraints;
136:
137: jSplitPane1 = new javax.swing.JSplitPane();
138: optionsPanel = new javax.swing.JPanel();
139: categoryLabel = new javax.swing.JLabel();
140: categoryCombo = new javax.swing.JComboBox();
141: categoryPanel = new javax.swing.JPanel();
142: previewPanel = new javax.swing.JPanel();
143: jScrollPane1 = new javax.swing.JScrollPane();
144: previewPane = new javax.swing.JEditorPane();
145:
146: setLayout(new java.awt.GridBagLayout());
147:
148: jSplitPane1.setBorder(null);
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.setPreferredSize(new java.awt.Dimension(300, 20));
191: previewPanel.setLayout(new java.awt.GridBagLayout());
192:
193: jScrollPane1.setDoubleBuffered(true);
194:
195: previewPane.setEditable(false);
196: jScrollPane1.setViewportView(previewPane);
197:
198: gridBagConstraints = new java.awt.GridBagConstraints();
199: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
200: gridBagConstraints.weightx = 1.0;
201: gridBagConstraints.weighty = 1.0;
202: previewPanel.add(jScrollPane1, gridBagConstraints);
203:
204: jSplitPane1.setRightComponent(previewPanel);
205:
206: gridBagConstraints = new java.awt.GridBagConstraints();
207: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
208: gridBagConstraints.weightx = 1.0;
209: gridBagConstraints.weighty = 1.0;
210: gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
211: add(jSplitPane1, gridBagConstraints);
212: }// </editor-fold>//GEN-END:initComponents
213:
214: // Variables declaration - do not modify//GEN-BEGIN:variables
215: private javax.swing.JComboBox categoryCombo;
216: private javax.swing.JLabel categoryLabel;
217: private javax.swing.JPanel categoryPanel;
218: private javax.swing.JScrollPane jScrollPane1;
219: private javax.swing.JSplitPane jSplitPane1;
220: private javax.swing.JPanel optionsPanel;
221: private javax.swing.JEditorPane previewPane;
222: private javax.swing.JPanel previewPanel;
223:
224: // End of variables declaration//GEN-END:variables
225:
226: private void createCategories() {
227: categories.add(FmtTabsIndents.getController());
228: // TODO - other categories?
229:
230: for (Category category : categories) {
231: category.addPropertyChangeListener(this );
232: }
233:
234: }
235:
236: // Change in the combo
237: public void actionPerformed(ActionEvent e) {
238: Category category = (Category) categoryCombo.getSelectedItem();
239: categoryPanel.setVisible(false);
240: categoryPanel.removeAll();
241: categoryPanel.add(category.getComponent(null),
242: BorderLayout.CENTER);
243: categoryPanel.setVisible(true);
244: if (loaded) {
245: repaintPreview();
246: }
247: }
248:
249: public static abstract class Category extends
250: OptionsPanelController {
251:
252: private String displayName;
253:
254: public Category(String displayNameKey) {
255: this .displayName = NbBundle.getMessage(
256: FormatingOptionsPanel.class, displayNameKey);
257: }
258:
259: public abstract void storeTo(Preferences preferences);
260:
261: public abstract void refreshPreview(JEditorPane pane,
262: Preferences p);
263:
264: @Override
265: public String toString() {
266: return displayName;
267: }
268:
269: }
270:
271: // Change in some of the subpanels
272: public void propertyChange(PropertyChangeEvent evt) {
273:
274: if (!loaded) {
275: return;
276: }
277:
278: // Notify the main controler that the page has changed
279: fopControler.changed();
280:
281: // Repaint the preview
282: repaintPreview();
283: }
284:
285: // XXX Only temporary
286:
287: private void repaintPreview() {
288:
289: Preferences p = new PreviewPreferences();
290:
291: for (Category category : categories) {
292: category.storeTo(p);
293: }
294:
295: Category category = (Category) categoryCombo.getSelectedItem();
296: jScrollPane1.setIgnoreRepaint(true);
297: category.refreshPreview(previewPane, p);
298: previewPane.setIgnoreRepaint(false);
299: previewPane.scrollRectToVisible(new Rectangle(0, 0, 10, 10));
300: previewPane.repaint(100);
301:
302: FmtOptions.lastValues = p;
303:
304: }
305:
306: public static class PreviewPreferences extends AbstractPreferences {
307:
308: private Map<String, Object> map = new HashMap<String, Object>();
309:
310: public PreviewPreferences() {
311: super (null, ""); // NOI18N
312: }
313:
314: protected void putSpi(String key, String value) {
315: map.put(key, value);
316: }
317:
318: protected String getSpi(String key) {
319: return (String) map.get(key);
320: }
321:
322: protected void removeSpi(String key) {
323: map.remove(key);
324: }
325:
326: protected void removeNodeSpi() throws BackingStoreException {
327: throw new UnsupportedOperationException(
328: "Not supported yet.");
329: }
330:
331: protected String[] keysSpi() throws BackingStoreException {
332: String array[] = new String[map.keySet().size()];
333: return map.keySet().toArray(array);
334: }
335:
336: protected String[] childrenNamesSpi()
337: throws BackingStoreException {
338: throw new UnsupportedOperationException(
339: "Not supported yet.");
340: }
341:
342: protected AbstractPreferences childSpi(String name) {
343: throw new UnsupportedOperationException(
344: "Not supported yet.");
345: }
346:
347: protected void syncSpi() throws BackingStoreException {
348: throw new UnsupportedOperationException(
349: "Not supported yet.");
350: }
351:
352: protected void flushSpi() throws BackingStoreException {
353: throw new UnsupportedOperationException(
354: "Not supported yet.");
355: }
356:
357: }
358:
359: }
|