2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.java.ui;
43
import java.awt.BorderLayout;
44
import java.awt.Rectangle;
45
import java.awt.event.ActionEvent;
46
import java.awt.event.ActionListener;
47
import java.beans.PropertyChangeEvent;
48
import java.beans.PropertyChangeListener;
49
import java.util.ArrayList;
50
import java.util.HashMap;
51
import java.util.List;
53
import java.util.prefs.AbstractPreferences;
54
import java.util.prefs.BackingStoreException;
55
import java.util.prefs.Preferences;
56
import javax.swing.DefaultComboBoxModel;
57
import javax.swing.JEditorPane;
58
import javax.swing.JPanel;
59
import javax.swing.UIManager;
60
import org.netbeans.spi.options.OptionsPanelController;
61
import org.openide.util.NbBundle;
67
public class FormatingOptionsPanel extends JPanel implements ActionListener, PropertyChangeListener {
69
private FormatingOptionsPanelController fopControler;
70
private List<FormatingOptionsPanel.Category> categories = new ArrayList<FormatingOptionsPanel.Category>();
72
private boolean loaded = false;
74
/** Creates new form FormatingOptionsPanel */
75
public FormatingOptionsPanel( FormatingOptionsPanelController fopControler ) {
76
this.fopControler = fopControler;
80
if( "Windows".equals(UIManager.getLookAndFeel().getID()) ) //NOI18N
83
previewPane.setContentType("text/x-java"); // NOI18N
84
// Don't highlight caret row
85
previewPane.putClientProperty(
86
"HighlightsLayerExcludes", // NOI18N
87
"^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$" // NOI18N
89
previewPane.setText("1234567890123456789012345678901234567890"); // NOI18N
90
previewPane.setDoubleBuffered(true);
93
DefaultComboBoxModel model = new DefaultComboBoxModel();
94
for (Category category : categories) {
95
model.addElement(category);
97
categoryCombo.setModel(model);
100
categoryCombo.addActionListener(this);
101
actionPerformed(new ActionEvent(model, 0, null));
106
for (Category category : categories) {
114
for (Category category : categories) {
115
category.applyChanges();
118
FmtOptions.lastValues = null;
122
FmtOptions.lastValues = null;
125
/** This method is called from within the constructor to
126
* initialize the form.
127
* WARNING: Do NOT modify this code. The content of this method is
128
* always regenerated by the Form Editor.
130
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
131
private void initComponents() {
132
java.awt.GridBagConstraints gridBagConstraints;
134
jSplitPane1 = new javax.swing.JSplitPane();
135
optionsPanel = new javax.swing.JPanel();
136
categoryLabel = new javax.swing.JLabel();
137
categoryCombo = new javax.swing.JComboBox();
138
categoryPanel = new javax.swing.JPanel();
139
previewPanel = new javax.swing.JPanel();
140
jScrollPane1 = new javax.swing.JScrollPane();
141
previewPane = new javax.swing.JEditorPane();
143
setLayout(new java.awt.GridBagLayout());
145
jSplitPane1.setBorder(null);
146
jSplitPane1.setDividerLocation(400);
147
jSplitPane1.setOpaque(false);
149
optionsPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 8));
150
optionsPanel.setOpaque(false);
151
optionsPanel.setLayout(new java.awt.GridBagLayout());
153
categoryLabel.setLabelFor(categoryCombo);
154
org.openide.awt.Mnemonics.setLocalizedText(categoryLabel, org.openide.util.NbBundle.getMessage(FormatingOptionsPanel.class, "LBL_Category")); // NOI18N
155
gridBagConstraints = new java.awt.GridBagConstraints();
156
gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
157
optionsPanel.add(categoryLabel, gridBagConstraints);
159
categoryCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
160
gridBagConstraints = new java.awt.GridBagConstraints();
161
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
162
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
163
gridBagConstraints.weightx = 1.0;
164
gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
165
optionsPanel.add(categoryCombo, gridBagConstraints);
167
categoryPanel.setOpaque(false);
168
categoryPanel.setLayout(new java.awt.BorderLayout());
169
gridBagConstraints = new java.awt.GridBagConstraints();
170
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
171
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
172
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
173
gridBagConstraints.weightx = 1.0;
174
gridBagConstraints.weighty = 1.0;
175
optionsPanel.add(categoryPanel, gridBagConstraints);
177
jSplitPane1.setLeftComponent(optionsPanel);
179
previewPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 8, 0, 0));
180
previewPanel.setOpaque(false);
181
previewPanel.setLayout(new java.awt.GridBagLayout());
183
jScrollPane1.setDoubleBuffered(true);
185
previewPane.setEditable(false);
186
jScrollPane1.setViewportView(previewPane);
188
gridBagConstraints = new java.awt.GridBagConstraints();
189
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
190
gridBagConstraints.weightx = 1.0;
191
gridBagConstraints.weighty = 1.0;
192
previewPanel.add(jScrollPane1, gridBagConstraints);
194
jSplitPane1.setRightComponent(previewPanel);
196
gridBagConstraints = new java.awt.GridBagConstraints();
197
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
198
gridBagConstraints.weightx = 1.0;
199
gridBagConstraints.weighty = 1.0;
200
gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
201
add(jSplitPane1, gridBagConstraints);
202
}// </editor-fold>//GEN-END:initComponents
205
// Variables declaration - do not modify//GEN-BEGIN:variables
206
private javax.swing.JComboBox categoryCombo;
207
private javax.swing.JLabel categoryLabel;
208
private javax.swing.JPanel categoryPanel;
209
private javax.swing.JScrollPane jScrollPane1;
210
private javax.swing.JSplitPane jSplitPane1;
211
private javax.swing.JPanel optionsPanel;
212
private javax.swing.JEditorPane previewPane;
213
private javax.swing.JPanel previewPanel;
214
// End of variables declaration//GEN-END:variables
217
private void createCategories() {
218
categories.add(FmtTabsIndents.getController());
219
// categories.add(FmtCodeGeneration.getController());
220
categories.add(FmtAlignmentBraces.getController());
221
categories.add(FmtWrapping.getController());
222
categories.add(FmtBlankLines.getController());
223
categories.add(FmtSpaces.getController());
224
// categories.add(FmtImports.getController()); // XXX
226
for (Category category : categories) {
227
category.addPropertyChangeListener(this);
233
// Change in the combo
234
public void actionPerformed(ActionEvent e) {
235
Category category = (Category)categoryCombo.getSelectedItem();
236
categoryPanel.setVisible(false);
237
categoryPanel.removeAll();
238
categoryPanel.add(category.getComponent(null), BorderLayout.CENTER);
239
categoryPanel.setVisible(true);
245
public static abstract class Category extends OptionsPanelController {
247
private String displayName;
249
public Category(String displayNameKey) {
250
this.displayName = NbBundle.getMessage( FormatingOptionsPanel.class, displayNameKey );
253
public abstract void storeTo(Preferences preferences);
255
public abstract void refreshPreview(JEditorPane pane, Preferences p);
258
public String toString() {
265
// Change in some of the subpanels
266
public void propertyChange(PropertyChangeEvent evt) {
272
// Notify the main controler that the page has changed
273
fopControler.changed();
275
// Repaint the preview
279
// XXX Only temporary
282
private void repaintPreview() {
284
Preferences p = new PreviewPreferences();
287
for (Category category : categories) {
291
Category category = (Category)categoryCombo.getSelectedItem();
292
jScrollPane1.setIgnoreRepaint(true);
293
category.refreshPreview(previewPane, p);
294
previewPane.setIgnoreRepaint(false);
295
previewPane.scrollRectToVisible(new Rectangle(0,0,10,10) );
296
previewPane.repaint(100);
298
FmtOptions.lastValues = p;
302
public static class PreviewPreferences extends AbstractPreferences {
304
private Map<String,Object> map = new HashMap<String, Object>();
306
public PreviewPreferences() {
307
super(null, ""); // NOI18N
310
protected void putSpi(String key, String value) {
314
protected String getSpi(String key) {
315
return (String)map.get(key);
318
protected void removeSpi(String key) {
322
protected void removeNodeSpi() throws BackingStoreException {
323
throw new UnsupportedOperationException("Not supported yet.");
326
protected String[] keysSpi() throws BackingStoreException {
327
String array[] = new String[map.keySet().size()];
328
return map.keySet().toArray( array );
331
protected String[] childrenNamesSpi() throws BackingStoreException {
332
throw new UnsupportedOperationException("Not supported yet.");
335
protected AbstractPreferences childSpi(String name) {
336
throw new UnsupportedOperationException("Not supported yet.");
339
protected void syncSpi() throws BackingStoreException {
340
throw new UnsupportedOperationException("Not supported yet.");
343
protected void flushSpi() throws BackingStoreException {
344
throw new UnsupportedOperationException("Not supported yet.");