Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2011 Sun Microsystems, Inc. |
41 |
*/ |
42 |
|
43 |
package org.netbeans.modules.maven.options; |
44 |
|
45 |
import java.awt.EventQueue; |
46 |
import java.awt.event.ActionEvent; |
47 |
import java.beans.PropertyChangeListener; |
48 |
import java.lang.ref.Reference; |
49 |
import java.lang.ref.WeakReference; |
50 |
import javax.swing.AbstractAction; |
51 |
import javax.swing.JCheckBoxMenuItem; |
52 |
import javax.swing.JComponent; |
53 |
import javax.swing.JSeparator; |
54 |
import org.netbeans.api.project.FileOwnerQuery; |
55 |
import org.netbeans.api.project.Project; |
56 |
import org.netbeans.modules.maven.ActionProviderImpl; |
57 |
import org.openide.awt.DynamicMenuContent; |
58 |
import org.openide.awt.Mnemonics; |
59 |
import org.openide.filesystems.FileObject; |
60 |
import org.openide.loaders.DataObject; |
61 |
import org.openide.util.Lookup; |
62 |
import org.openide.util.LookupEvent; |
63 |
import org.openide.util.LookupListener; |
64 |
import org.openide.util.NbBundle; |
65 |
import org.openide.util.Utilities; |
66 |
|
67 |
/** |
68 |
* Action which displays a checkbox and can add/remove --offline from the |
69 |
* default Maven arguments. |
70 |
* |
71 |
* @author Tim Boudreau |
72 |
*/ |
73 |
public final class MavenOfflineAction extends AbstractAction implements LookupListener, DynamicMenuContent { |
74 |
private Lookup.Result<DataObject> result; |
75 |
private Reference<JCheckBoxMenuItem> menuItem = null; |
76 |
private final Object lock = new Object(); |
77 |
private boolean updatingMenuItem; |
78 |
|
79 |
public MavenOfflineAction() { |
80 |
//This class could be massively simplified if the line below |
81 |
//worked for BooleanStateAction |
82 |
// putValue (DynamicMenuContent.HIDE_WHEN_DISABLED, Boolean.TRUE); |
83 |
putValue (NAME, NbBundle.getMessage(MavenOfflineAction.class, |
84 |
"ACTION_MAVEN_TOGGLE_OFFLINE")); //NOI18N |
85 |
} |
86 |
|
87 |
private void addNotify() { |
88 |
Lookup.Result<DataObject> res = Utilities.actionsGlobalContext().lookupResult(DataObject.class); |
89 |
synchronized(lock) { |
90 |
this.result = res; |
91 |
} |
92 |
res.addLookupListener(this); |
93 |
res.allItems(); |
94 |
resultChanged(null); |
95 |
updatingMenuItem = false; |
96 |
} |
97 |
|
98 |
private void removeNotify() { |
99 |
Lookup.Result<DataObject> res; |
100 |
synchronized(lock) { |
101 |
res = this.result; |
102 |
this.result = null; |
103 |
} |
104 |
if (res != null) { //yes, this sometimes *is* called asymmetrically and the bug is not here |
105 |
res.removeLookupListener(this); |
106 |
} |
107 |
} |
108 |
|
109 |
private boolean getBooleanState() { |
110 |
Boolean offline = MavenSettings.getDefault().isOffline(); |
111 |
return offline == null ? false : offline.booleanValue(); |
112 |
} |
113 |
|
114 |
private void setBooleanState(boolean value) { |
115 |
MavenSettings.getDefault().setOffline(value); |
116 |
} |
117 |
|
118 |
private JCheckBoxMenuItem getMenuPresenter() { |
119 |
assert EventQueue.isDispatchThread(); |
120 |
JCheckBoxMenuItem item = menuItem == null ? null : menuItem.get(); |
121 |
if (item == null) { |
122 |
item = new JCheckBoxMenuItem(this); //Do NOT use Actions.connect() here! |
123 |
Mnemonics.setLocalizedText(item, getValue(NAME).toString()); |
124 |
menuItem = new WeakReference<JCheckBoxMenuItem>(item); |
125 |
updatingMenuItem = true; |
126 |
try { |
127 |
item.setSelected(getBooleanState()); |
128 |
} finally { |
129 |
updatingMenuItem = false; |
130 |
} |
131 |
} |
132 |
return item; |
133 |
} |
134 |
|
135 |
@Override |
136 |
public JComponent[] getMenuPresenters() { |
137 |
return isEnabled() ? new JComponent[] { |
138 |
new JSeparator(), getMenuPresenter(), new JSeparator() |
139 |
} : new JComponent[0]; |
140 |
} |
141 |
|
142 |
@Override |
143 |
public JComponent[] synchMenuPresenters(JComponent[] items) { |
144 |
assert EventQueue.isDispatchThread(); |
145 |
if (items.length == 3 && items[2] instanceof JCheckBoxMenuItem) { |
146 |
updatingMenuItem = true; |
147 |
try { |
148 |
((JCheckBoxMenuItem) items[2]).setSelected(getBooleanState()); |
149 |
} finally { |
150 |
updatingMenuItem = false; |
151 |
} |
152 |
return items; |
153 |
} else { |
154 |
return getMenuPresenters(); |
155 |
} |
156 |
} |
157 |
|
158 |
@Override |
159 |
@SuppressWarnings("unchecked") |
160 |
public void resultChanged(LookupEvent ev) { |
161 |
Lookup.Result<DataObject> res; |
162 |
if (ev == null) { |
163 |
synchronized (lock) { |
164 |
res = this.result; |
165 |
} |
166 |
if (res == null) { //can happen |
167 |
return; |
168 |
} |
169 |
} else { |
170 |
res = (Lookup.Result<DataObject>) ev.getSource(); |
171 |
} |
172 |
boolean hasProject = false; |
173 |
for (DataObject dob : res.allInstances()) { |
174 |
FileObject fo = dob.getPrimaryFile(); |
175 |
Project p = FileOwnerQuery.getOwner(fo); |
176 |
hasProject = p.getLookup().lookup(ActionProviderImpl.class) != null; |
177 |
if (hasProject) { |
178 |
break; |
179 |
} |
180 |
} |
181 |
setEnabled(hasProject); |
182 |
} |
183 |
|
184 |
@Override |
185 |
public void actionPerformed(ActionEvent e) { |
186 |
if (updatingMenuItem) { |
187 |
return; |
188 |
} |
189 |
Boolean val = MavenSettings.getDefault().isOffline(); |
190 |
boolean offline = val == null ? false : val.booleanValue(); |
191 |
setBooleanState(!offline); |
192 |
} |
193 |
|
194 |
@Override |
195 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
196 |
boolean added; |
197 |
synchronized (this) { |
198 |
super.addPropertyChangeListener(listener); |
199 |
added = getPropertyChangeListeners().length == 1; |
200 |
} |
201 |
if (added) { |
202 |
addNotify(); |
203 |
} |
204 |
} |
205 |
|
206 |
@Override |
207 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
208 |
boolean removed; |
209 |
synchronized (this) { |
210 |
removed = getPropertyChangeListeners().length == 1; |
211 |
super.removePropertyChangeListener(listener); |
212 |
} |
213 |
if (removed) { |
214 |
removeNotify(); |
215 |
} |
216 |
} |
217 |
|
218 |
|
219 |
} |