Issue 16: Add UI for user defined actions
[nbgit.git] / src / org / nbgit / ui / custom / CustomMenu.java
blobc00a3d4cc16e76bec898887d9c1cbd4c3de7458c
1 /*
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]"
23 * Contributor(s):
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;
38 import java.io.File;
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;
45 import org.nbgit.Git;
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;
55 /**
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)) {
71 menu.addSeparator();
73 if (addRepositoryActions(menu, builder)) {
74 menu.addSeparator();
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
85 return menu;
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());
97 int actions = 0;
99 for (String action : config.getSubsections("nbgit")) {
100 if (action.startsWith("action") && load(builder, config, action)) {
101 menu.add(builder.build());
102 actions++;
106 return actions > 0;
109 public boolean addMainActions(JMenu menu, CustomActionBuilder builder) {
110 Preferences prefs = GitModuleConfig.getDefault().getPreferences();
111 HashSet<String> seen = new HashSet<String>();
112 String[] keys;
114 try {
115 keys = prefs.keys();
116 } catch (BackingStoreException ex) {
117 return false;
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());
126 seen.add(key);
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) {
157 super(context);
160 @Override
161 public CustomAction build() {
162 if (!isValid()) {
163 return null;
166 int i = 0;
167 if (isRepoSpecific()) {
168 RepositoryConfig config = getRepositoryConfig(getContext());
169 Collection<String> subsections = config.getSubsections("nbgit");
171 while (subsections.contains("action" + i)) {
172 i++;
175 for (Option option : Option.values()) {
176 String value = this.getValue(option);
177 config.setString("nbgit", "action" + i, option.name(), value);
180 try {
181 config.save();
182 } catch (IOException ex) {
184 } else {
185 Preferences prefs = GitModuleConfig.getDefault().getPreferences();
187 while (prefs.get(getPreferencesName(Option.name, i), null) != null) {
188 i++;
191 for (Option option : Option.values()) {
192 prefs.put(getPreferencesName(option, i), getValue(option));
195 try {
196 prefs.sync();
197 } catch (BackingStoreException ex) {
201 return super.build();