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-2006 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.welcome.content;
043:
044: import java.awt.BorderLayout;
045: import java.awt.GridBagConstraints;
046: import java.awt.GridBagLayout;
047: import java.awt.Insets;
048: import java.awt.event.ActionEvent;
049: import java.beans.PropertyChangeEvent;
050: import java.beans.PropertyChangeListener;
051: import java.io.IOException;
052: import java.net.URL;
053: import java.util.List;
054: import javax.swing.AbstractAction;
055: import javax.swing.JLabel;
056: import javax.swing.JPanel;
057: import org.netbeans.api.project.Project;
058: import org.netbeans.api.project.ProjectManager;
059: import org.netbeans.api.project.ui.OpenProjects;
060: import org.netbeans.modules.project.ui.api.RecentProjects;
061: import org.netbeans.modules.project.ui.api.UnloadedProjectInformation;
062: import org.openide.DialogDescriptor;
063: import org.openide.DialogDisplayer;
064: import org.openide.NotifyDescriptor;
065: import org.openide.filesystems.FileObject;
066: import org.openide.filesystems.URLMapper;
067: import org.openide.util.NbBundle;
068:
069: /**
070: * Panel showing all recent projects as clickable buttons.
071: *
072: * @author S. Aubrecht
073: */
074: public class RecentProjectsPanel extends JPanel implements Constants {
075:
076: private static final int MAX_PROJECTS = 10;
077: private PropertyChangeListener changeListener;
078:
079: /** Creates a new instance of RecentProjectsPanel */
080: public RecentProjectsPanel() {
081: super (new BorderLayout());
082: setOpaque(false);
083: }
084:
085: @Override
086: public void addNotify() {
087: super .addNotify();
088: removeAll();
089: add(rebuildContent(), BorderLayout.CENTER);
090: RecentProjects.getDefault().addPropertyChangeListener(
091: getPropertyChangeListener());
092: }
093:
094: @Override
095: public void removeNotify() {
096: super .removeNotify();
097: RecentProjects.getDefault().removePropertyChangeListener(
098: getPropertyChangeListener());
099: }
100:
101: private PropertyChangeListener getPropertyChangeListener() {
102: if (null == changeListener) {
103: changeListener = new PropertyChangeListener() {
104: public void propertyChange(PropertyChangeEvent e) {
105: if (RecentProjects.PROP_RECENT_PROJECT_INFO
106: .equals(e.getPropertyName())) {
107: removeAll();
108: add(rebuildContent(), BorderLayout.CENTER);
109: invalidate();
110: revalidate();
111: repaint();
112: }
113: }
114: };
115: }
116: return changeListener;
117: }
118:
119: private JPanel rebuildContent() {
120: JPanel panel = new JPanel(new GridBagLayout());
121: panel.setOpaque(false);
122: int row = 0;
123: List<UnloadedProjectInformation> projects = RecentProjects
124: .getDefault().getRecentProjectInformation();
125: for (UnloadedProjectInformation p : projects) {
126: addProject(panel, row++, p);
127: if (row >= MAX_PROJECTS)
128: break;
129: }
130: if (0 == row) {
131: panel.add(new JLabel(BundleSupport
132: .getLabel("NoRecentProject")), //NOI18N
133: new GridBagConstraints(0, row, 1, 1, 1.0, 1.0,
134: GridBagConstraints.CENTER,
135: GridBagConstraints.NONE, new Insets(10, 10,
136: 10, 10), 0, 0));
137: } else {
138: panel.add(new JLabel(), new GridBagConstraints(0, row, 1,
139: 1, 0.0, 1.0, GridBagConstraints.CENTER,
140: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0,
141: 0));
142: }
143: return panel;
144: }
145:
146: private void addProject(JPanel panel, int row,
147: final UnloadedProjectInformation project) {
148: OpenProjectAction action = new OpenProjectAction(project);
149: ActionButton b = new ActionButton(action, true, project
150: .getURL().toString());
151: b.setFont(BUTTON_FONT);
152: b.getAccessibleContext().setAccessibleName(b.getText());
153: b.getAccessibleContext().setAccessibleDescription(
154: BundleSupport.getAccessibilityDescription(
155: "RecentProject", b.getText())); //NOI18N
156: panel.add(b, new GridBagConstraints(0, row, 1, 1, 1.0, 0.0,
157: GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
158: new Insets(2, 2, 2, 2), 0, 0));
159: }
160:
161: private class OpenProjectAction extends AbstractAction {
162: private UnloadedProjectInformation project;
163:
164: public OpenProjectAction(UnloadedProjectInformation project) {
165: super (project.getDisplayName(), project.getIcon());
166: this .project = project;
167: }
168:
169: public void actionPerformed(ActionEvent e) {
170: URL url = project.getURL();
171: Project prj = null;
172:
173: FileObject dir = URLMapper.findFileObject(url);
174: if (dir != null && dir.isFolder()) {
175: try {
176: prj = ProjectManager.getDefault().findProject(dir);
177: } catch (IOException ioEx) {
178: // Ignore invalid folders
179: }
180: }
181:
182: if (prj != null) {
183: OpenProjects.getDefault().open(new Project[] { prj },
184: false);
185: } else {
186: String msg = BundleSupport.getMessage(
187: "ERR_InvalidProject", project.getDisplayName()); //NOI18N
188: NotifyDescriptor nd = new NotifyDescriptor.Message(msg);
189: DialogDisplayer.getDefault().notify(nd);
190: rebuildContent();
191: }
192: }
193: }
194: }
|