2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2008 Jonas Fonseca <fonseca@diku.dk>
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. See the License for the
12 * specific language governing permissions and limitations under the
13 * License. When distributing the software, include this License Header
14 * Notice in each file.
16 * This particular file is subject to the "Classpath" exception as provided
17 * by Sun in the GPL Version 2 section of the License file that
18 * accompanied this code. If applicable, add the following below the
19 * License Header, with the fields enclosed by brackets [] replaced by
20 * your own identifying information:
21 * "Portions Copyrighted [year] [name of copyright owner]"
25 * If you wish your version of this file to be governed by only the CDDL
26 * or only the GPL Version 2, indicate your decision by adding
27 * "[Contributor] elects to include this software in this distribution
28 * under the [CDDL or GPL Version 2] license." If you do not indicate a
29 * single choice of license, a recipient has the option to distribute
30 * your version of this file under either the CDDL, the GPL Version 2 or
31 * to extend the choice of license to its licensees as provided above.
32 * However, if you add GPL Version 2 code and therefore, elected the GPL
33 * Version 2 license, then the option applies only if the new code is
34 * made subject to such option by the copyright holder.
36 package org
.nbgit
.ui
.custom
;
39 import java
.io
.IOException
;
40 import java
.util
.Collection
;
41 import java
.util
.HashSet
;
42 import java
.util
.prefs
.BackingStoreException
;
43 import java
.util
.prefs
.Preferences
;
44 import javax
.swing
.JMenu
;
46 import org
.nbgit
.GitModuleConfig
;
47 import org
.nbgit
.ui
.ContextMenu
;
48 import org
.nbgit
.ui
.custom
.CustomActionBuilder
.Option
;
49 import org
.nbgit
.util
.GitUtils
;
50 import org
.netbeans
.modules
.versioning
.spi
.VCSContext
;
51 import org
.openide
.util
.NbBundle
;
52 import org
.spearce
.jgit
.lib
.Repository
;
53 import org
.spearce
.jgit
.lib
.RepositoryConfig
;
56 * Menu for custom actions.
58 public class CustomMenu
extends ContextMenu
{
60 private static final String preferencesPrefix
= "action";
62 public CustomMenu(VCSContext ctx
, boolean mainMenu
) {
63 super(NbBundle
.getMessage(CustomMenu
.class, "CustomMenu"), ctx
, mainMenu
);
66 protected JMenu
createMenu(VCSContext context
, boolean mainMenu
) {
67 CustomActionBuilder builder
= CustomActionBuilder
.newBuilder(context
);
68 JMenu menu
= new JMenu(this);
70 if (addMainActions(menu
, builder
)) {
73 if (addRepositoryActions(menu
, builder
)) {
77 builder
= new CustomWizardActionBuilder(context
);
78 builder
.setRepoSpecific(!mainMenu
);
79 String name
= mainMenu
80 ? NbBundle
.getMessage(CustomMenu
.class, "NewMainAction") // NOI18N
81 : NbBundle
.getMessage(CustomMenu
.class, "NewRepoAction"); // NOI18N
82 menu
.add(new CustomWizardAction(name
, builder
));
83 org
.openide
.awt
.Mnemonics
.setLocalizedText(menu
, NbBundle
.getMessage(CustomMenu
.class, "CustomMenu")); // NOI18N
88 private RepositoryConfig
getRepositoryConfig(VCSContext context
) {
89 File root
= GitUtils
.getRootFile(context
);
90 Repository repo
= Git
.getInstance().getRepository(root
);
92 return repo
== null ?
null : repo
.getConfig();
95 public boolean addRepositoryActions(JMenu menu
, CustomActionBuilder builder
) {
96 RepositoryConfig config
= getRepositoryConfig(builder
.getContext());
99 for (String action
: config
.getSubsections("nbgit")) {
100 if (action
.startsWith("action") && load(builder
, config
, action
)) {
101 menu
.add(builder
.build());
109 public boolean addMainActions(JMenu menu
, CustomActionBuilder builder
) {
110 Preferences prefs
= GitModuleConfig
.getDefault().getPreferences();
111 HashSet
<String
> seen
= new HashSet
<String
>();
116 } catch (BackingStoreException ex
) {
120 for (String key
: keys
) {
121 int cutOffset
= key
.indexOf(".");
122 if (key
.startsWith(preferencesPrefix
) && cutOffset
!= -1) {
123 key
= key
.substring(0, cutOffset
);
124 if (!seen
.contains(key
) && load(builder
, prefs
, key
)) {
125 menu
.add(builder
.build());
131 return !seen
.isEmpty();
134 private String
getPreferencesName(Option option
, int i
) {
135 return preferencesPrefix
+ i
+ "." + option
.name();
138 public boolean load(CustomActionBuilder builder
, RepositoryConfig config
, String subsection
) {
139 for (Option option
: Option
.values()) {
140 String value
= config
.getString("nbgit", subsection
, option
.name());
141 builder
.setOption(option
, value
);
143 return builder
.isValid();
146 public boolean load(CustomActionBuilder builder
, Preferences prefs
, String key
) {
147 for (Option option
: Option
.values()) {
148 String value
= prefs
.get(key
+ "." + option
.name(), null);
149 builder
.setOption(option
, value
);
151 return builder
.isValid();
154 private class CustomWizardActionBuilder
extends CustomActionBuilder
{
156 CustomWizardActionBuilder(VCSContext context
) {
161 public CustomAction
build() {
167 if (isRepoSpecific()) {
168 RepositoryConfig config
= getRepositoryConfig(getContext());
169 Collection
<String
> subsections
= config
.getSubsections("nbgit");
171 while (subsections
.contains("action" + i
)) {
175 for (Option option
: Option
.values()) {
176 String value
= this.getValue(option
);
177 config
.setString("nbgit", "action" + i
, option
.name(), value
);
182 } catch (IOException ex
) {
185 Preferences prefs
= GitModuleConfig
.getDefault().getPreferences();
187 while (prefs
.get(getPreferencesName(Option
.name
, i
), null) != null) {
191 for (Option option
: Option
.values()) {
192 prefs
.put(getPreferencesName(option
, i
), getValue(option
));
197 } catch (BackingStoreException ex
) {
201 return super.build();