Cleanup code whitespace
[nbgit.git] / src / org / nbgit / options / GitOptionsPanelController.java
blob9861f2f926e53304b7f77be1b8d62fcf74a2f927
1 /*
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]"
24 * Contributor(s):
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.
29 * Portions Copyright 2008 Alexander Coles (Ikonoklastik Productions).
31 * If you wish your version of this file to be governed by only the CDDL
32 * or only the GPL Version 2, indicate your decision by adding
33 * "[Contributor] elects to include this software in this distribution
34 * under the [CDDL or GPL Version 2] license." If you do not indicate a
35 * single choice of license, a recipient has the option to distribute
36 * your version of this file under either the CDDL, the GPL Version 2 or
37 * to extend the choice of license to its licensees as provided above.
38 * However, if you add GPL Version 2 code and therefore, elected the GPL
39 * Version 2 license, then the option applies only if the new code is
40 * made subject to such option by the copyright holder.
42 package org.nbgit.options;
44 import java.awt.BorderLayout;
45 import java.awt.Dialog;
46 import java.awt.event.ActionEvent;
47 import java.awt.event.ActionListener;
48 import java.awt.event.MouseAdapter;
49 import java.awt.event.MouseEvent;
50 import java.beans.PropertyChangeListener;
51 import java.beans.PropertyChangeSupport;
52 import java.io.File;
53 import java.util.ArrayList;
54 import java.util.List;
55 import javax.swing.JButton;
56 import javax.swing.JComponent;
57 import javax.swing.JFileChooser;
58 import javax.swing.JOptionPane;
59 import javax.swing.filechooser.FileFilter;
60 import org.nbgit.Git;
61 import org.nbgit.GitAnnotator;
62 import org.nbgit.GitModuleConfig;
63 import org.nbgit.util.HtmlFormatter;
64 import org.netbeans.modules.versioning.util.AccessibleJFileChooser;
65 import org.netbeans.spi.options.OptionsPanelController;
66 import org.openide.DialogDescriptor;
67 import org.openide.DialogDisplayer;
68 import org.openide.filesystems.FileUtil;
69 import org.openide.util.HelpCtx;
70 import org.openide.util.Lookup;
71 import org.openide.util.NbBundle;
73 final class GitOptionsPanelController extends OptionsPanelController implements ActionListener {
75 private GitPanel panel;
76 private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
77 private boolean changed;
79 public GitOptionsPanelController()
81 panel = new GitPanel(this);
82 panel.exportFilenameBrowseButton.addActionListener(this);
84 String tooltip = NbBundle.getMessage(GitPanel.class, "GitPanel.annotationTextField.toolTipText", GitAnnotator.LABELS); // NOI18N
86 panel.annotationTextField.setToolTipText(tooltip);
87 panel.addButton.addActionListener(this);
90 public void update()
92 getPanel().load();
93 changed = false;
96 public void applyChanges()
98 if (!validateFields())
99 return;
101 getPanel().store();
102 // {folder} variable setting
103 HtmlFormatter.getInstance().refresh();
104 Git.getInstance().refreshAllAnnotations();
106 changed = false;
109 public void cancel()
111 // need not do anything special, if no changes have been persisted yet
114 public boolean isValid()
116 return getPanel().valid();
119 public boolean isChanged()
121 return changed;
124 public HelpCtx getHelpCtx()
126 return new HelpCtx(GitOptionsPanelController.class);
129 public JComponent getComponent(Lookup masterLookup)
131 return getPanel();
134 public void addPropertyChangeListener(PropertyChangeListener l)
136 pcs.addPropertyChangeListener(l);
139 public void removePropertyChangeListener(PropertyChangeListener l)
141 pcs.removePropertyChangeListener(l);
144 public void actionPerformed(ActionEvent evt)
146 if (evt.getSource() == panel.exportFilenameBrowseButton)
147 onExportFilenameBrowseClick();
148 else if (evt.getSource() == panel.addButton)
149 onAddClick();
152 private File getExportFile()
154 String execPath = panel.exportFilenameTextField.getText();
155 return FileUtil.normalizeFile(new File(execPath));
158 private Boolean validateFields()
160 String username = panel.emailTextField.getText();
161 if (!GitModuleConfig.getDefault().isUserNameValid(username)) {
162 JOptionPane.showMessageDialog(null,
163 NbBundle.getMessage(GitPanel.class, "MSG_WARN_USER_NAME_TEXT"), // NOI18N
164 NbBundle.getMessage(GitPanel.class, "MSG_WARN_FIELD_TITLE"), // NOI18N
165 JOptionPane.WARNING_MESSAGE);
166 return false;
168 return true;
171 private void onExportFilenameBrowseClick()
173 File oldFile = getExportFile();
174 JFileChooser fileChooser = new AccessibleJFileChooser(NbBundle.getMessage(GitOptionsPanelController.class, "ACSD_ExportBrowseFolder"), oldFile); // NOI18N
175 fileChooser.setDialogTitle(NbBundle.getMessage(GitOptionsPanelController.class, "ExportBrowse_title")); // NOI18N
176 fileChooser.setMultiSelectionEnabled(false);
177 FileFilter[] old = fileChooser.getChoosableFileFilters();
178 for (int i = 0; i < old.length; i++) {
179 FileFilter fileFilter = old[i];
180 fileChooser.removeChoosableFileFilter(fileFilter);
182 fileChooser.showDialog(panel, NbBundle.getMessage(GitOptionsPanelController.class, "OK_Button")); // NOI18N
183 File f = fileChooser.getSelectedFile();
184 if (f != null)
185 panel.exportFilenameTextField.setText(f.getAbsolutePath());
188 private GitPanel getPanel()
190 if (panel == null)
191 panel = new GitPanel(this);
192 return panel;
195 void changed()
197 if (!changed) {
198 changed = true;
199 pcs.firePropertyChange(OptionsPanelController.PROP_CHANGED, false, true);
201 pcs.firePropertyChange(OptionsPanelController.PROP_VALID, null, null);
204 private class LabelVariable {
206 private String description;
207 private String variable;
209 public LabelVariable(String variable, String description)
211 this.description = description;
212 this.variable = variable;
215 @Override
216 public String toString()
218 return description;
221 public String getDescription()
223 return description;
226 public String getVariable()
228 return variable;
233 private void onAddClick()
235 LabelsPanel labelsPanel = new LabelsPanel();
236 List<LabelVariable> variables = new ArrayList<LabelVariable>(GitAnnotator.LABELS.length);
237 for (int i = 0; i < GitAnnotator.LABELS.length; i++) {
238 LabelVariable variable = new LabelVariable(
239 GitAnnotator.LABELS[i],
240 "{" + GitAnnotator.LABELS[i] + "} - " + NbBundle.getMessage(GitPanel.class, "GitPanel.label." + GitAnnotator.LABELS[i]) // NOI18N
242 variables.add(variable);
244 labelsPanel.labelsList.setListData(variables.toArray(new LabelVariable[variables.size()]));
246 String title = NbBundle.getMessage(GitPanel.class, "GitPanel.labelVariables.title"); // NOI18N
247 String acsd = NbBundle.getMessage(GitPanel.class, "GitPanel.labelVariables.acsd"); // NOI18N
249 DialogDescriptor dialogDescriptor = new DialogDescriptor(labelsPanel, title);
250 dialogDescriptor.setModal(true);
251 dialogDescriptor.setValid(true);
253 final Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
254 dialog.getAccessibleContext().setAccessibleDescription(acsd);
256 labelsPanel.labelsList.addMouseListener(new MouseAdapter() {
258 @Override
259 public void mouseClicked(MouseEvent e)
261 if (e.getClickCount() == 2)
262 dialog.setVisible(false);
267 dialog.setVisible(true);
269 if (DialogDescriptor.OK_OPTION.equals(dialogDescriptor.getValue())) {
271 Object[] selection = labelsPanel.labelsList.getSelectedValues();
273 String variable = ""; // NOI18N
274 for (int i = 0; i < selection.length; i++) {
275 variable += "{" + ((LabelVariable) selection[i]).getVariable() + "}"; // NOI18N
278 String annotation = panel.annotationTextField.getText();
280 int pos = panel.annotationTextField.getCaretPosition();
281 if (pos < 0)
282 pos = annotation.length();
284 StringBuffer sb = new StringBuffer(annotation.length() + variable.length());
285 sb.append(annotation.substring(0, pos));
286 sb.append(variable);
287 if (pos < annotation.length())
288 sb.append(annotation.substring(pos, annotation.length()));
289 panel.annotationTextField.setText(sb.toString());
290 panel.annotationTextField.requestFocus();
291 panel.annotationTextField.setCaretPosition(pos + variable.length());
295 private void onManageClick()
297 final PropertiesPanel panel = new PropertiesPanel();
299 final PropertiesTable propTable;
301 propTable = new PropertiesTable(panel.labelForTable, PropertiesTable.PROPERTIES_COLUMNS, new String[]{PropertiesTableModel.COLUMN_NAME_VALUE});
303 panel.setPropertiesTable(propTable);
305 JComponent component = propTable.getComponent();
307 panel.propsPanel.setLayout(new BorderLayout());
309 panel.propsPanel.add(component, BorderLayout.CENTER);
311 GitExtProperties gitProperties = new GitExtProperties(panel, propTable, null);
313 DialogDescriptor dd = new DialogDescriptor(panel, NbBundle.getMessage(GitOptionsPanelController.class, "CTL_PropertiesDialog_Title", null), true, null); // NOI18N
314 final JButton okButton = new JButton(NbBundle.getMessage(GitOptionsPanelController.class, "CTL_Properties_Action_OK")); // NOI18N
315 okButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(GitOptionsPanelController.class, "CTL_Properties_Action_OK")); // NOI18N
316 final JButton cancelButton = new JButton(NbBundle.getMessage(GitOptionsPanelController.class, "CTL_Properties_Action_Cancel")); // NOI18N
317 cancelButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(GitOptionsPanelController.class, "CTL_Properties_Action_Cancel")); // NOI18N
318 dd.setOptions(new Object[]{okButton, cancelButton}); // NOI18N
319 dd.setHelpCtx(new HelpCtx(GitOptionsPanelController.class));
320 panel.putClientProperty("contentTitle", null); // NOI18N
321 panel.putClientProperty("DialogDescriptor", dd); // NOI18N
322 Dialog dialog = DialogDisplayer.getDefault().createDialog(dd);
323 dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(GitOptionsPanelController.class, "CTL_PropertiesDialog_Title")); // NOI18N
325 dialog.pack();
326 dialog.setVisible(true);
327 if (dd.getValue() == okButton)
328 gitProperties.setProperties();