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