This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 194036
Collapse All | Expand All

(-)a/maven/manifest.mf (-1 / +1 lines)
Lines 1-6 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.netbeans.modules.maven/2
2
OpenIDE-Module: org.netbeans.modules.maven/2
3
OpenIDE-Module-Specification-Version: 2.4
3
OpenIDE-Module-Specification-Version: 2.5
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/maven/Bundle.properties
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/maven/Bundle.properties
5
OpenIDE-Module-Layer: org/netbeans/modules/maven/layer.xml
5
OpenIDE-Module-Layer: org/netbeans/modules/maven/layer.xml
6
AutoUpdate-Show-In-Client: false
6
AutoUpdate-Show-In-Client: false
(-)a/maven/src/org/netbeans/modules/maven/layer.xml (+7 lines)
Lines 254-257 Link Here
254
            </file>
254
            </file>
255
        </folder>
255
        </folder>
256
    </folder>
256
    </folder>
257
    <folder name="Menu">
258
        <folder name="BuildProject">
259
            <file name="org-netbeans-modules-maven-options-MavenOfflineAction.instance">
260
                <attr intvalue="65532" name="position"/>
261
            </file>
262
        </folder>
263
    </folder>
257
</filesystem>
264
</filesystem>
(-)a/maven/src/org/netbeans/modules/maven/options/Bundle.properties (+1 lines)
Lines 94-96 Link Here
94
MAVEN_RUNTIME_External={0}
94
MAVEN_RUNTIME_External={0}
95
MAVEN_RUNTIME_Browse=Browse...
95
MAVEN_RUNTIME_Browse=Browse...
96
KW_MavenOptions=Maven Version,Maven Options,Maven Repository Options
96
KW_MavenOptions=Maven Version,Maven Options,Maven Repository Options
97
ACTION_MAVEN_TOGGLE_OFFLINE=Maven &Offline Mode
(-)a/maven/src/org/netbeans/modules/maven/options/MavenOfflineAction.java (+219 lines)
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
}
(-)a/maven/src/org/netbeans/modules/maven/options/MavenSettings.java (-1 / +38 lines)
Lines 50-56 Link Here
50
import java.io.InputStream;
50
import java.io.InputStream;
51
import java.net.URL;
51
import java.net.URL;
52
import java.util.ArrayList;
52
import java.util.ArrayList;
53
import java.util.Arrays;
53
import java.util.Collections;
54
import java.util.Collections;
55
import java.util.Iterator;
54
import java.util.List;
56
import java.util.List;
55
import java.util.Properties;
57
import java.util.Properties;
56
import java.util.logging.Logger;
58
import java.util.logging.Logger;
Lines 62-68 Link Here
62
import org.openide.filesystems.FileUtil;
64
import org.openide.filesystems.FileUtil;
63
import org.openide.filesystems.URLMapper;
65
import org.openide.filesystems.URLMapper;
64
import org.openide.modules.InstalledFileLocator;
66
import org.openide.modules.InstalledFileLocator;
65
import org.openide.util.NbBundle;
67
import org.openide.util.Exceptions;
66
import org.openide.util.NbPreferences;
68
import org.openide.util.NbPreferences;
67
69
68
/**
70
/**
Lines 118-123 Link Here
118
    public boolean isUpdateSnapshots() {
120
    public boolean isUpdateSnapshots() {
119
        return hasOption("--update-snapshots", "-U"); //NOI18N
121
        return hasOption("--update-snapshots", "-U"); //NOI18N
120
    }
122
    }
123
    
124
    private static final String OFFLINE_ARGUMENT_LONG = "--offline";
125
    private static final String OFFLINE_ARGUMENT_SHORT = "-o";
126
    public void setOffline(boolean val) {
127
        boolean currentState = hasOption (OFFLINE_ARGUMENT_LONG, OFFLINE_ARGUMENT_SHORT);
128
        if (currentState != val) {
129
            String defOpts = getDefaultOptions();
130
            if (val) {
131
                if (defOpts == null) {
132
                    setDefaultOptions(OFFLINE_ARGUMENT_LONG);
133
                } else {
134
                    defOpts = OFFLINE_ARGUMENT_LONG + ' ' + defOpts; //NOI18N
135
                    setDefaultOptions(defOpts);
136
                }
137
            } else {
138
                if (defOpts != null) {
139
                    try {
140
                        String[] strs = CommandLineUtils.translateCommandline(defOpts);
141
                        StringBuilder sb = new StringBuilder();
142
                        for (String curr : strs) {
143
                            if (!OFFLINE_ARGUMENT_SHORT.equals(curr) && !OFFLINE_ARGUMENT_LONG.equals(curr)) {
144
                                if (sb.length() > 0 && sb.charAt(sb.length() - 1) != ' ') { //NOI18N
145
                                    sb.append(' ');
146
                                }
147
                                sb.append(curr);
148
                            }
149
                        }
150
                        setDefaultOptions(sb.toString());
151
                    } catch (Exception ex) {
152
                        Exceptions.printStackTrace(ex);
153
                    }
154
                }
155
            }
156
        }
157
    }
121
158
122
159
123
    public boolean hasOption(String longName, String shortName) {
160
    public boolean hasOption(String longName, String shortName) {

Return to bug 194036