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.subversion.options;
042:
043: import java.awt.Dialog;
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.awt.event.MouseAdapter;
047: import java.awt.event.MouseEvent;
048: import java.io.File;
049: import java.util.ArrayList;
050: import java.util.List;
051: import javax.swing.JFileChooser;
052: import org.netbeans.modules.subversion.Annotator;
053: import org.netbeans.modules.subversion.Subversion;
054: import org.netbeans.modules.subversion.SvnModuleConfig;
055: import org.netbeans.modules.subversion.ui.repository.Repository;
056: import org.netbeans.modules.versioning.util.AccessibleJFileChooser;
057: import org.netbeans.spi.options.OptionsPanelController;
058: import org.openide.DialogDescriptor;
059: import org.openide.DialogDisplayer;
060: import org.openide.filesystems.FileUtil;
061: import org.openide.util.HelpCtx;
062: import org.openide.util.NbBundle;
063:
064: /**
065: *
066: * @author Tomas Stupka
067: */
068: public final class SvnOptionsController extends OptionsPanelController
069: implements ActionListener {
070:
071: private final SvnOptionsPanel panel;
072: private final Repository repository;
073: private final AnnotationSettings annotationSettings;
074:
075: public SvnOptionsController() {
076:
077: int repositoryModeMask = Repository.FLAG_URL_ENABLED
078: | Repository.FLAG_SHOW_REMOVE;
079: String title = org.openide.util.NbBundle.getMessage(
080: SvnOptionsController.class, "CTL_Repository_Location");
081: repository = new Repository(repositoryModeMask, title); // NOI18N
082:
083: annotationSettings = new AnnotationSettings();
084:
085: panel = new SvnOptionsPanel();
086: panel.browseButton.addActionListener(this );
087: panel.manageConnSettingsButton.addActionListener(this );
088: panel.manageLabelsButton.addActionListener(this );
089:
090: String tooltip = NbBundle
091: .getMessage(
092: AnnotationSettings.class,
093: "AnnotationSettingsPanel.annotationTextField.toolTipText",
094: Annotator.LABELS);
095: panel.annotationTextField.setToolTipText(tooltip);
096: panel.addButton.addActionListener(this );
097: }
098:
099: public void update() {
100:
101: panel.executablePathTextField.setText(SvnModuleConfig
102: .getDefault().getExecutableBinaryPath());
103: panel.annotationTextField.setText(SvnModuleConfig.getDefault()
104: .getAnnotationFormat());
105:
106: annotationSettings.update();
107: repository.refreshUrlHistory();
108:
109: }
110:
111: public void applyChanges() {
112: // executable
113: SvnModuleConfig.getDefault().setExecutableBinaryPath(
114: panel.executablePathTextField.getText());
115: SvnModuleConfig.getDefault().setAnnotationFormat(
116: panel.annotationTextField.getText());
117:
118: // {folder} variable setting
119: annotationSettings.applyChanges();
120: Subversion.getInstance().getAnnotator().refresh();
121: Subversion.getInstance().refreshAllAnnotations();
122:
123: // connection
124: repository.storeRecentUrls();
125: }
126:
127: public void cancel() {
128: repository.refreshUrlHistory();
129: }
130:
131: public boolean isValid() {
132: return true;
133: }
134:
135: public boolean isChanged() {
136: return !panel.executablePathTextField.getText().equals(
137: SvnModuleConfig.getDefault().getExecutableBinaryPath())
138: || !panel.annotationTextField.getText().equals(
139: SvnModuleConfig.getDefault()
140: .getAnnotationFormat())
141: || repository.isChanged()
142: || annotationSettings.isChanged();
143: }
144:
145: public org.openide.util.HelpCtx getHelpCtx() {
146: return new org.openide.util.HelpCtx(getClass());
147: }
148:
149: public javax.swing.JComponent getComponent(
150: org.openide.util.Lookup masterLookup) {
151: return panel;
152: }
153:
154: public void addPropertyChangeListener(
155: java.beans.PropertyChangeListener l) {
156:
157: }
158:
159: public void removePropertyChangeListener(
160: java.beans.PropertyChangeListener l) {
161:
162: }
163:
164: public void actionPerformed(ActionEvent evt) {
165: if (evt.getSource() == panel.browseButton) {
166: onBrowseClick();
167: } else if (evt.getSource() == panel.manageConnSettingsButton) {
168: onManageConnClick();
169: } else if (evt.getSource() == panel.manageLabelsButton) {
170: onManageLabelsClick();
171: } else if (evt.getSource() == panel.addButton) {
172: onAddClick();
173: }
174: }
175:
176: private File getExecutableFile() {
177: String execPath = panel.executablePathTextField.getText();
178: return FileUtil.normalizeFile(new File(execPath));
179: }
180:
181: private void onBrowseClick() {
182: File oldFile = getExecutableFile();
183: JFileChooser fileChooser = new AccessibleJFileChooser(NbBundle
184: .getMessage(SvnOptionsController.class,
185: "ACSD_BrowseFolder"), oldFile); // NOI18N
186: fileChooser.setDialogTitle(NbBundle.getMessage(
187: SvnOptionsController.class, "Browse_title")); // NOI18N
188: fileChooser.setMultiSelectionEnabled(false);
189: fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
190: fileChooser.showDialog(panel, NbBundle.getMessage(
191: SvnOptionsController.class, "OK_Button")); // NOI18N
192: File f = fileChooser.getSelectedFile();
193: if (f != null) {
194: panel.executablePathTextField.setText(f.getAbsolutePath());
195: }
196: }
197:
198: private void onManageConnClick() {
199: boolean ok = repository.show(NbBundle.getMessage(
200: SvnOptionsController.class, "CTL_ManageConnections"),
201: new HelpCtx(Repository.class), true);
202: if (!ok) {
203: repository.refreshUrlHistory();
204: }
205: }
206:
207: private void onManageLabelsClick() {
208: String labelFormat = panel.annotationTextField.getText()
209: .replaceAll(" ", "");
210: annotationSettings.show(labelFormat != null
211: && labelFormat.indexOf("{folder}") > -1);
212: }
213:
214: private class LabelVariable {
215: private String description;
216: private String variable;
217:
218: public LabelVariable(String variable, String description) {
219: this .description = description;
220: this .variable = variable;
221: }
222:
223: @Override
224: public String toString() {
225: return description;
226: }
227:
228: public String getDescription() {
229: return description;
230: }
231:
232: public String getVariable() {
233: return variable;
234: }
235: }
236:
237: private void onAddClick() {
238: LabelsPanel labelsPanel = new LabelsPanel();
239: List<LabelVariable> variables = new ArrayList<LabelVariable>(
240: Annotator.LABELS.length);
241: for (int i = 0; i < Annotator.LABELS.length; i++) {
242: LabelVariable variable = new LabelVariable(
243: Annotator.LABELS[i], "{"
244: + Annotator.LABELS[i]
245: + "} - "
246: + NbBundle.getMessage(
247: AnnotationSettings.class,
248: "AnnotationSettings.label."
249: + Annotator.LABELS[i]));
250: variables.add(variable);
251: }
252: labelsPanel.labelsList.setListData(variables
253: .toArray(new LabelVariable[variables.size()]));
254:
255: String title = NbBundle.getMessage(AnnotationSettings.class,
256: "AnnotationSettings.labelVariables.title");
257: String acsd = NbBundle.getMessage(AnnotationSettings.class,
258: "AnnotationSettings.labelVariables.acsd");
259:
260: DialogDescriptor dialogDescriptor = new DialogDescriptor(
261: labelsPanel, title);
262: dialogDescriptor.setModal(true);
263: dialogDescriptor.setValid(true);
264:
265: final Dialog dialog = DialogDisplayer.getDefault()
266: .createDialog(dialogDescriptor);
267: dialog.getAccessibleContext().setAccessibleDescription(acsd);
268:
269: labelsPanel.labelsList.addMouseListener(new MouseAdapter() {
270: @Override
271: public void mouseClicked(MouseEvent e) {
272: if (e.getClickCount() == 2) {
273: dialog.setVisible(false);
274: }
275: }
276: });
277:
278: dialog.setVisible(true);
279:
280: if (DialogDescriptor.OK_OPTION.equals(dialogDescriptor
281: .getValue())) {
282:
283: Object[] selection = labelsPanel.labelsList
284: .getSelectedValues();
285:
286: String variable = "";
287: for (int i = 0; i < selection.length; i++) {
288: variable += "{"
289: + ((LabelVariable) selection[i]).getVariable()
290: + "}";
291: }
292:
293: String annotation = panel.annotationTextField.getText();
294:
295: int pos = panel.annotationTextField.getCaretPosition();
296: if (pos < 0)
297: pos = annotation.length();
298:
299: StringBuffer sb = new StringBuffer(annotation.length()
300: + variable.length());
301: sb.append(annotation.substring(0, pos));
302: sb.append(variable);
303: if (pos < annotation.length()) {
304: sb.append(annotation
305: .substring(pos, annotation.length()));
306: }
307: panel.annotationTextField.setText(sb.toString());
308: panel.annotationTextField.requestFocus();
309: panel.annotationTextField.setCaretPosition(pos
310: + variable.length());
311:
312: }
313: }
314:
315: }
|