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.mercurial.ui.update;
042:
043: import java.io.File;
044: import java.util.Set;
045: import java.util.Vector;
046: import java.util.LinkedHashSet;
047: import javax.swing.SwingUtilities;
048: import javax.swing.ComboBoxModel;
049: import javax.swing.DefaultComboBoxModel;
050: import org.netbeans.api.progress.ProgressHandleFactory;
051: import org.netbeans.api.progress.ProgressHandle;
052: import org.netbeans.modules.mercurial.HgModuleConfig;
053: import org.openide.util.RequestProcessor;
054: import org.openide.util.NbBundle;
055: import org.netbeans.modules.mercurial.util.HgCommand;
056:
057: /**
058: *
059: * @author Padraig O'Briain
060: */
061: public class RevertModificationsPanel extends javax.swing.JPanel {
062:
063: private File repository;
064: private File[] revertFiles;
065: private RequestProcessor.Task refreshViewTask;
066: private Thread refreshViewThread;
067: private static final RequestProcessor rp = new RequestProcessor(
068: "MercurialRevert", 1); // NOI18N
069:
070: private static final int HG_REVERT_TARGET_LIMIT = 100;
071:
072: /** Creates new form ReverModificationsPanel */
073: public RevertModificationsPanel(File repo, File[] files) {
074: repository = repo;
075: revertFiles = files;
076: refreshViewTask = rp.create(new RefreshViewTask());
077: initComponents();
078: refreshViewTask.schedule(0);
079: }
080:
081: public File[] getRevertFiles() {
082: return revertFiles;
083: }
084:
085: public boolean isBackupRequested() {
086: return doBackupChxBox.isSelected();
087: }
088:
089: public String getSelectedRevision() {
090: String revStr = (String) revisionsComboBox.getSelectedItem();
091: if (revStr != null) {
092: if (revStr.equals(NbBundle.getMessage(
093: RevertModificationsPanel.class,
094: "MSG_Revision_Default"))
095: || // NOI18N
096: revStr.equals(NbBundle.getMessage(
097: RevertModificationsPanel.class,
098: "MSG_Fetching_Revisions"))) { // NOI18N
099: revStr = null;
100: } else {
101: revStr = revStr.substring(0, revStr.indexOf(" ")); // NOI18N
102: }
103: }
104: return revStr;
105: }
106:
107: /** This method is called from within the constructor to
108: * initialize the form.
109: * WARNING: Do NOT modify this code. The content of this method is
110: * always regenerated by the Form Editor.
111: */
112: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
113: private void initComponents() {
114:
115: revisionsLabel = new javax.swing.JLabel();
116: revisionsComboBox = new javax.swing.JComboBox();
117: jLabel1 = new javax.swing.JLabel();
118: jLabel2 = new javax.swing.JLabel();
119: jPanel1 = new javax.swing.JPanel();
120: doBackupChxBox = new javax.swing.JCheckBox();
121:
122: revisionsLabel.setLabelFor(revisionsComboBox);
123: org.openide.awt.Mnemonics
124: .setLocalizedText(
125: revisionsLabel,
126: org.openide.util.NbBundle
127: .getMessage(
128: RevertModificationsPanel.class,
129: "RevertModificationsPanel.revisionsLabel.text")); // NOI18N
130:
131: jLabel1.setFont(new java.awt.Font("Dialog", 1, 11));
132: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
133: org.openide.util.NbBundle.getMessage(
134: RevertModificationsPanel.class,
135: "RevertModificationsPanel.infoLabel.text")); // NOI18N
136:
137: jLabel2.setForeground(new java.awt.Color(153, 153, 153));
138: org.openide.awt.Mnemonics.setLocalizedText(jLabel2,
139: org.openide.util.NbBundle.getMessage(
140: RevertModificationsPanel.class,
141: "RevertModificationsPanel.infoLabel2.text")); // NOI18N
142:
143: jPanel1.setBorder(javax.swing.BorderFactory
144: .createTitledBorder("Options"));
145:
146: org.openide.awt.Mnemonics
147: .setLocalizedText(
148: doBackupChxBox,
149: org.openide.util.NbBundle
150: .getMessage(
151: RevertModificationsPanel.class,
152: "RevertModificationsPanel.doBackupChxBox.text")); // NOI18N
153:
154: org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(
155: jPanel1);
156: jPanel1.setLayout(jPanel1Layout);
157: jPanel1Layout.setHorizontalGroup(jPanel1Layout
158: .createParallelGroup(
159: org.jdesktop.layout.GroupLayout.LEADING).add(
160: jPanel1Layout.createSequentialGroup()
161: .addContainerGap().add(doBackupChxBox)
162: .addContainerGap(57, Short.MAX_VALUE)));
163: jPanel1Layout.setVerticalGroup(jPanel1Layout
164: .createParallelGroup(
165: org.jdesktop.layout.GroupLayout.LEADING).add(
166: jPanel1Layout.createSequentialGroup().add(
167: doBackupChxBox).addContainerGap(10,
168: Short.MAX_VALUE)));
169:
170: doBackupChxBox.getAccessibleContext().setAccessibleDescription(
171: org.openide.util.NbBundle.getMessage(
172: RevertModificationsPanel.class,
173: "ACSD_doBackupChxBox")); // NOI18N
174:
175: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
176: this );
177: this .setLayout(layout);
178: layout
179: .setHorizontalGroup(layout
180: .createParallelGroup(
181: org.jdesktop.layout.GroupLayout.LEADING)
182: .add(
183: layout
184: .createSequentialGroup()
185: .add(
186: layout
187: .createParallelGroup(
188: org.jdesktop.layout.GroupLayout.TRAILING)
189: .add(
190: org.jdesktop.layout.GroupLayout.LEADING,
191: layout
192: .createSequentialGroup()
193: .add(
194: 47,
195: 47,
196: 47)
197: .add(
198: revisionsLabel)
199: .addPreferredGap(
200: org.jdesktop.layout.LayoutStyle.RELATED)
201: .add(
202: revisionsComboBox,
203: 0,
204: 182,
205: Short.MAX_VALUE))
206: .add(
207: org.jdesktop.layout.GroupLayout.LEADING,
208: layout
209: .createSequentialGroup()
210: .addContainerGap()
211: .add(
212: layout
213: .createParallelGroup(
214: org.jdesktop.layout.GroupLayout.LEADING)
215: .add(
216: jLabel2)
217: .add(
218: jLabel1,
219: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
220: 363,
221: Short.MAX_VALUE)
222: .add(
223: jPanel1,
224: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
225: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
226: Short.MAX_VALUE))))
227: .addContainerGap()));
228: layout
229: .setVerticalGroup(layout
230: .createParallelGroup(
231: org.jdesktop.layout.GroupLayout.LEADING)
232: .add(
233: layout
234: .createSequentialGroup()
235: .add(
236: jLabel1,
237: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
238: 25,
239: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
240: .add(4, 4, 4)
241: .add(jLabel2)
242: .add(29, 29, 29)
243: .add(
244: layout
245: .createParallelGroup(
246: org.jdesktop.layout.GroupLayout.BASELINE)
247: .add(
248: revisionsLabel)
249: .add(
250: revisionsComboBox,
251: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
252: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
253: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
254: .addPreferredGap(
255: org.jdesktop.layout.LayoutStyle.UNRELATED)
256: .add(
257: jPanel1,
258: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
259: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
260: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
261: .addContainerGap(
262: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
263: Short.MAX_VALUE)));
264:
265: revisionsComboBox.getAccessibleContext()
266: .setAccessibleDescription(
267: org.openide.util.NbBundle.getMessage(
268: RevertModificationsPanel.class,
269: "ACSD_revisionsComboBox")); // NOI18N
270: }// </editor-fold>//GEN-END:initComponents
271:
272: /**
273: * Must NOT be run from AWT.
274: */
275: private void setupModels() {
276: // XXX attach Cancelable hook
277: final ProgressHandle ph = ProgressHandleFactory
278: .createHandle(NbBundle.getMessage(
279: RevertModificationsPanel.class,
280: "MSG_Refreshing_Revert_Versions")); // NOI18N
281: try {
282: boolean doBackup = HgModuleConfig.getDefault()
283: .getBackupOnRevertModifications();
284: doBackupChxBox.setSelected(doBackup);
285:
286: Set<String> initialRevsSet = new LinkedHashSet<String>();
287: initialRevsSet.add(NbBundle.getMessage(
288: RevertModificationsPanel.class,
289: "MSG_Fetching_Revisions")); // NOI18N
290: ComboBoxModel targetsModel = new DefaultComboBoxModel(
291: new Vector<String>(initialRevsSet));
292: revisionsComboBox.setModel(targetsModel);
293: refreshViewThread = Thread.currentThread();
294: Thread.interrupted(); // clear interupted status
295: ph.start();
296:
297: refreshRevisions();
298: } finally {
299: SwingUtilities.invokeLater(new Runnable() {
300: public void run() {
301: ph.finish();
302: refreshViewThread = null;
303: }
304: });
305: }
306: }
307:
308: private void refreshRevisions() {
309: java.util.List<String> targetRevsList = HgCommand
310: .getRevisionsForFile(repository, revertFiles,
311: HG_REVERT_TARGET_LIMIT);
312:
313: Set<String> targetRevsSet = new LinkedHashSet<String>();
314:
315: int size;
316: if (targetRevsList == null) {
317: size = 0;
318: targetRevsSet.add(NbBundle.getMessage(
319: RevertModificationsPanel.class,
320: "MSG_Revision_Default")); // NOI18N
321: } else {
322: size = targetRevsList.size();
323: int i = 0;
324: while (i < size) {
325: targetRevsSet.add(targetRevsList.get(i));
326: i++;
327: }
328: }
329: ComboBoxModel targetsModel = new DefaultComboBoxModel(
330: new Vector<String>(targetRevsSet));
331: revisionsComboBox.setModel(targetsModel);
332:
333: if (targetRevsSet.size() > 0) {
334: revisionsComboBox.setSelectedIndex(0);
335: }
336: }
337:
338: private class RefreshViewTask implements Runnable {
339: public void run() {
340: setupModels();
341: }
342: }
343:
344: // Variables declaration - do not modify//GEN-BEGIN:variables
345: private javax.swing.JCheckBox doBackupChxBox;
346: private javax.swing.JLabel jLabel1;
347: private javax.swing.JLabel jLabel2;
348: private javax.swing.JPanel jPanel1;
349: private javax.swing.JComboBox revisionsComboBox;
350: private javax.swing.JLabel revisionsLabel;
351: // End of variables declaration//GEN-END:variables
352:
353: }
|