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:
042: package org.netbeans.modules.mercurial.ui.log;
043:
044: import java.awt.event.ActionEvent;
045: import org.netbeans.modules.versioning.util.Utils;
046: import org.openide.nodes.Node;
047: import org.openide.util.NbBundle;
048: import javax.swing.*;
049: import java.io.File;
050: import java.util.*;
051: import org.netbeans.modules.mercurial.FileInformation;
052: import org.netbeans.modules.mercurial.OutputLogger;
053: import org.netbeans.modules.mercurial.ui.actions.ContextAction;
054: import org.netbeans.modules.mercurial.util.HgUtils;
055: import org.netbeans.modules.versioning.spi.VCSContext;
056: import org.openide.windows.TopComponent;
057:
058: /**
059: * Opens Search History Component.
060: *
061: * @author Maros Sandor
062: */
063: public class SearchHistoryAction extends ContextAction {
064: private final VCSContext context;
065: static final int DIRECTORY_ENABLED_STATUS = FileInformation.STATUS_MANAGED
066: & ~FileInformation.STATUS_NOTVERSIONED_EXCLUDED
067: & ~FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY;
068: static final int FILE_ENABLED_STATUS = FileInformation.STATUS_MANAGED
069: & ~FileInformation.STATUS_NOTVERSIONED_EXCLUDED
070: & ~FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY;
071:
072: public SearchHistoryAction(String name, VCSContext context) {
073: this .context = context;
074: putValue(Action.NAME, name);
075: }
076:
077: protected String getBaseName(Node[] activatedNodes) {
078: return "CTL_MenuItem_SearchHistory"; // NOI18N
079: }
080:
081: protected int getFileEnabledStatus() {
082: return FILE_ENABLED_STATUS;
083: }
084:
085: protected int getDirectoryEnabledStatus() {
086: return DIRECTORY_ENABLED_STATUS;
087: }
088:
089: protected boolean asynchronous() {
090: return false;
091: }
092:
093: public void performAction(ActionEvent e) {
094: String title = NbBundle.getMessage(SearchHistoryAction.class,
095: "CTL_SearchHistory_Title", Utils
096: .getContextDisplayName(context)); // NOI18N
097: openHistory(context, title);
098: }
099:
100: public static void openHistory(final VCSContext context,
101: final String title) {
102: SwingUtilities.invokeLater(new Runnable() {
103: public void run() {
104: if (context == null)
105: return;
106: outputSearchContextTab(context, "MSG_Log_Title");
107: SearchHistoryTopComponent tc = new SearchHistoryTopComponent(
108: context);
109: tc.setDisplayName(title);
110: tc.open();
111: tc.requestActive();
112: }
113: });
114: }
115:
116: private static void outputSearchContextTab(VCSContext context,
117: String title) {
118: File root = HgUtils.getRootFile(context);
119: OutputLogger logger = OutputLogger.getLogger(root
120: .getAbsolutePath());
121: logger.outputInRed(NbBundle.getMessage(
122: SearchHistoryAction.class, title));
123: logger.outputInRed(NbBundle.getMessage(
124: SearchHistoryAction.class, "MSG_Log_Title_Sep")); // NOI18N
125: File[] files = context.getFiles().toArray(new File[0]);
126: logger.output(NbBundle.getMessage(SearchHistoryAction.class,
127: "MSG_LOG_CONTEXT_SEP")); // NOI18N
128: for (File f : files) {
129: logger.output(f.getAbsolutePath());
130: }
131: logger.outputInRed(""); // NOI18N
132: logger.closeLog();
133: }
134:
135: /**
136: * Opens the Seach History panel to view Mercurial Incoming Changesets that will be sent on next Pull from remote repo
137: * using: hg incoming - to get the data
138: *
139: * @param title title of the search
140: * @param commitMessage commit message to search for
141: * @param username user name to search for
142: * @param date date of the change in question
143: */
144: public static void openIncoming(final VCSContext context,
145: final String title) {
146: SwingUtilities.invokeLater(new Runnable() {
147: public void run() {
148: if (context == null)
149: return;
150: outputSearchContextTab(context, "MSG_LogIncoming_Title");
151: SearchHistoryTopComponent tc = new SearchHistoryTopComponent(
152: context);
153: tc.setDisplayName(title);
154: tc.open();
155: tc.requestActive();
156: tc.searchIncoming();
157: }
158: });
159: }
160:
161: /**
162: * Opens the Seach History panel to view Mercurial Out Changesets that will be sent on next Push to remote repo
163: * using: hg out - to get the data
164: *
165: * @param title title of the search
166: * @param commitMessage commit message to search for
167: * @param username user name to search for
168: * @param date date of the change in question
169: */
170: public static void openOut(final VCSContext context,
171: final String title) {
172: SwingUtilities.invokeLater(new Runnable() {
173: public void run() {
174: if (context == null)
175: return;
176: outputSearchContextTab(context, "MSG_LogOut_Title");
177: SearchHistoryTopComponent tc = new SearchHistoryTopComponent(
178: context);
179: tc.setDisplayName(title);
180: tc.open();
181: tc.requestActive();
182: tc.searchOut();
183: }
184: });
185: }
186:
187: /**
188: * Opens the Seach History panel with given pre-filled values. The search is executed in default context
189: * (all open projects).
190: *
191: * @param title title of the search
192: * @param commitMessage commit message to search for
193: * @param username user name to search for
194: * @param date date of the change in question
195: */
196: public static void openSearch(String title, String commitMessage,
197: String username, Date date) {
198: openSearch(getDefaultContext(), title, commitMessage, username,
199: date);
200: }
201:
202: public static void openSearch(VCSContext context, String title,
203: String commitMessage, String username, Date date) {
204: Calendar c = Calendar.getInstance();
205: c.setTime(date);
206: // annotations do not include time information, we must search whole day
207: c.add(Calendar.DATE, 1);
208: Date to = c.getTime();
209: c.setTime(date);
210: c.add(Calendar.DATE, -1);
211: Date from = c.getTime();
212:
213: if (commitMessage != null && commitMessage.indexOf('\n') != -1) {
214: commitMessage = commitMessage.substring(0, commitMessage
215: .indexOf('\n'));
216: }
217: SearchHistoryTopComponent tc = new SearchHistoryTopComponent(
218: context, commitMessage, username, from, to);
219: String tcTitle = NbBundle.getMessage(SearchHistoryAction.class,
220: "CTL_SearchHistory_Title", title); // NOI18N
221: tc.setDisplayName(tcTitle);
222: tc.open();
223: tc.requestActive();
224: tc.search();
225: }
226:
227: private static VCSContext getDefaultContext() {
228: Node[] nodes = TopComponent.getRegistry().getActivatedNodes();
229:
230: return nodes != null ? VCSContext.forNodes(nodes)
231: : VCSContext.EMPTY;
232: }
233:
234: /**
235: * Opens search panel in the context of the given repository URL.
236: *
237: * @param repositoryUrl URL to search
238: * @param localRoot local working copy root that corresponds to the repository URL
239: * @param revision revision to search for
240: */
241: public static void openSearch(String repositoryUrl, File localRoot,
242: long revision) {
243: SearchHistoryTopComponent tc = new SearchHistoryTopComponent(
244: repositoryUrl, localRoot, revision);
245: String tcTitle = NbBundle.getMessage(SearchHistoryAction.class,
246: "CTL_SearchHistory_Title", repositoryUrl); // NOI18N
247: tc.setDisplayName(tcTitle);
248: tc.open();
249: tc.requestActive();
250: tc.search();
251: }
252:
253: }
|