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-2007 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.autoupdate.ui;
043:
044: import java.net.URL;
045: import java.text.Collator;
046: import java.util.ArrayList;
047: import java.util.Collections;
048: import java.util.Comparator;
049: import java.util.HashSet;
050: import java.util.List;
051: import java.util.Set;
052: import java.util.logging.Logger;
053: import javax.swing.table.AbstractTableModel;
054: import org.netbeans.api.autoupdate.UpdateUnitProvider;
055: import org.netbeans.api.autoupdate.UpdateUnitProvider;
056: import org.netbeans.api.autoupdate.UpdateUnitProviderFactory;
057: import org.openide.util.NbBundle;
058: import org.openide.util.RequestProcessor;
059:
060: /**
061: *
062: * @author Radek Matous
063: */
064: public class SettingsTableModel extends AbstractTableModel {
065: private static final String[] COLUMN_NAME_KEYS = new String[] {
066: "SettingsTable_ActiveColumn", "SettingsTable_NameColumn",
067: /*"SettingsTable_URLColumn"*/
068: };
069:
070: private static final Class[] COLUMN_TYPES = new Class[] {
071: Boolean.class, UpdateUnitProvider.class,
072: /*String.class*/
073: };
074: private List<UpdateUnitProvider> updateProviders;
075: private Set<String> originalProviders;
076: private SettingsTab settingsTab = null;
077:
078: private final Logger logger = Logger
079: .getLogger("org.netbeans.modules.autoupdate.ui.SettingsTableModel");
080:
081: /** Creates a new instance of SettingsTableModel */
082: public SettingsTableModel() {
083: refreshModel();
084: }
085:
086: void setSettingsTab(SettingsTab settingsTab) {
087: this .settingsTab = settingsTab;
088: }
089:
090: SettingsTab getSettingsTab() {
091: return settingsTab;
092: }
093:
094: void refreshModel() {
095: Set<String> oldValue = originalProviders;
096: Set<String> newValue = new HashSet<String>();
097: final List<UpdateUnitProvider> forRefresh = new ArrayList<UpdateUnitProvider>();
098: List<UpdateUnitProvider> providers = UpdateUnitProviderFactory
099: .getDefault().getUpdateUnitProviders(false);
100: for (UpdateUnitProvider p : providers) {
101: if (oldValue != null && !oldValue.contains(p.getName())) {
102: // new one provider
103: if (p.isEnabled()) {
104: forRefresh.add(p);
105: }
106: }
107: newValue.add(p.getName());
108: }
109: if (!forRefresh.isEmpty()) {
110: getSettingsTab().setWaitingState(true);
111: Utilities.startAsWorkerThread(new Runnable() {
112: public void run() {
113: try {
114: Utilities.presentRefreshProviders(forRefresh,
115: getSettingsTab().getPluginManager(),
116: true);
117: getSettingsTab().getPluginManager()
118: .updateUnitsChanged();
119: } finally {
120: getSettingsTab().setWaitingState(false);
121: }
122: }
123: });
124: }
125: // check removed providers
126: if (oldValue != null && !oldValue.isEmpty()
127: && !newValue.containsAll(oldValue)) {
128: getSettingsTab().setWaitingState(true);
129: Utilities.startAsWorkerThread(new Runnable() {
130: public void run() {
131: try {
132: getSettingsTab().getPluginManager()
133: .updateUnitsChanged();
134: } finally {
135: getSettingsTab().setWaitingState(false);
136: }
137: }
138: });
139: }
140: updateProviders = new ArrayList<UpdateUnitProvider>(providers);
141: originalProviders = newValue;
142: sortAlphabetically(updateProviders);
143: fireTableDataChanged();
144: }
145:
146: public void remove(int rowIndex) {
147: UpdateUnitProvider unitProvider = getUpdateUnitProvider(rowIndex);
148: if (unitProvider != null) {
149: UpdateUnitProviderFactory.getDefault().remove(unitProvider);
150: }
151: getSettingsTab().getPluginManager().updateUnitsChanged();
152: }
153:
154: public void add(String name, String displayName, URL url,
155: boolean state) {
156: final UpdateUnitProvider uup = UpdateUnitProviderFactory
157: .getDefault().create(name, displayName, url);
158: uup.setEnable(state);
159: }
160:
161: public UpdateUnitProvider getUpdateUnitProvider(int rowIndex) {
162: return (rowIndex >= 0 && rowIndex < updateProviders.size()) ? updateProviders
163: .get(rowIndex)
164: : null;
165: }
166:
167: @Override
168: public boolean isCellEditable(int rowIndex, int columnIndex) {
169: return columnIndex == 0;
170: }
171:
172: public int getRowCount() {
173: return updateProviders.size();
174: }
175:
176: public int getColumnCount() {
177: return COLUMN_NAME_KEYS.length;
178: }
179:
180: @Override
181: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
182: final UpdateUnitProvider unitProvider = getUpdateUnitProvider(rowIndex);
183: switch (columnIndex) {
184: case 0:
185: boolean oldValue = unitProvider.isEnabled();
186: boolean newValue = ((Boolean) aValue).booleanValue();
187: if (oldValue != newValue) {
188: unitProvider.setEnable(newValue);
189: if (oldValue) {
190: RequestProcessor.getDefault().post(new Runnable() {
191: public void run() {
192: // was enabled and won't be more -> remove it from model
193: getSettingsTab().setWaitingState(true);
194: Utilities
195: .startAsWorkerThread(new Runnable() {
196: public void run() {
197: try {
198: getSettingsTab()
199: .getPluginManager()
200: .updateUnitsChanged();
201: } finally {
202: getSettingsTab()
203: .setWaitingState(
204: false);
205: }
206: }
207: });
208: }
209: });
210: } else {
211: // was enabled and won't be more -> add it from model and read its content
212: getSettingsTab().setWaitingState(true);
213: Utilities.startAsWorkerThread(new Runnable() {
214: public void run() {
215: try {
216: Utilities.presentRefreshProvider(
217: unitProvider, getSettingsTab()
218: .getPluginManager(),
219: false);
220: getSettingsTab().getPluginManager()
221: .updateUnitsChanged();
222: } finally {
223: getSettingsTab().setWaitingState(false);
224: }
225: }
226: });
227: }
228: }
229: break;
230: }
231: }
232:
233: public Object getValueAt(int rowIndex, int columnIndex) {
234: Object retval = null;
235: UpdateUnitProvider unitProvider = updateProviders.get(rowIndex);
236: switch (columnIndex) {
237: case 0:
238: retval = unitProvider.isEnabled();
239: break;
240: case 1:
241: retval = unitProvider;
242: break;
243: /*case 2: URL url = unitProvider.getProviderURL();
244: retval = (url != null) ? url.toExternalForm() : "";//NOI18N
245: break;*/
246: }
247: return retval;
248: }
249:
250: @Override
251: public Class<?> getColumnClass(int columnIndex) {
252: return COLUMN_TYPES[columnIndex];
253: }
254:
255: @Override
256: public String getColumnName(int columnIndex) {
257: return NbBundle.getMessage(SettingsTableModel.class,
258: COLUMN_NAME_KEYS[columnIndex]);
259: }
260:
261: private static void sortAlphabetically(List<UpdateUnitProvider> res) {
262: Collections.sort(res, new Comparator<UpdateUnitProvider>() {
263: Collator COLL = Collator.getInstance();
264:
265: public int compare(UpdateUnitProvider p1,
266: UpdateUnitProvider p2) {
267: return COLL.compare(p1.getDisplayName(), p2
268: .getDisplayName());
269: }
270: });
271:
272: }
273:
274: }
|