001: /*
002: * FindBugs - Find Bugs in Java programs
003: * Copyright (C) 2006, University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package edu.umd.cs.findbugs.gui2;
021:
022: import java.awt.BorderLayout;
023: import java.awt.event.ItemEvent;
024: import java.awt.event.ItemListener;
025: import java.awt.event.KeyEvent;
026: import java.io.ByteArrayInputStream;
027: import java.io.ByteArrayOutputStream;
028: import java.io.IOException;
029: import java.io.ObjectInputStream;
030: import java.io.ObjectOutputStream;
031: import java.util.ArrayList;
032: import java.util.HashMap;
033: import java.util.Map;
034:
035: import javax.swing.JCheckBoxMenuItem;
036: import javax.swing.JMenu;
037:
038: import net.infonode.docking.DockingWindow;
039: import net.infonode.docking.DockingWindowAdapter;
040: import net.infonode.docking.DockingWindowListener;
041: import net.infonode.docking.RootWindow;
042: import net.infonode.docking.SplitWindow;
043: import net.infonode.docking.TabWindow;
044: import net.infonode.docking.View;
045: import net.infonode.docking.theme.DockingWindowsTheme;
046: import net.infonode.docking.theme.ShapedGradientDockingTheme;
047: import net.infonode.docking.title.DockingWindowTitleProvider;
048: import net.infonode.docking.util.DockingUtil;
049: import net.infonode.docking.util.ViewMap;
050:
051: /**
052: * @author pugh
053: */
054: public class DockLayout implements FindBugsLayoutManager {
055: private static class DockParentListener extends
056: DockingWindowAdapter {
057: @Override
058: public void windowClosed(DockingWindow window) {
059: // Notify all children's listeners
060: ArrayList<DockingWindow> children = new ArrayList<DockingWindow>();
061: for (int i = 0; i < window.getChildWindowCount(); i++)
062: children.add(window.getChildWindow(i));
063: for (DockingWindow i : children)
064: i.close();
065: }
066: }
067:
068: private class ViewMenuItem extends JCheckBoxMenuItem implements
069: ItemListener {
070: private View view;
071:
072: public ViewMenuItem(View view, String title) {
073: super (title, true);
074: addItemListener(this );
075: this .view = view;
076: // view.addListener(new Listener());
077: }
078:
079: // Menu item has been checked or unchecked
080: public void itemStateChanged(ItemEvent evt) {
081: if (evt.getStateChange() == ItemEvent.SELECTED)
082: DockingUtil.addWindow(view, rootWindow);
083: if (evt.getStateChange() == ItemEvent.DESELECTED)
084: view.close();
085: }
086:
087: // private class Listener extends DockingWindowAdapter
088: // {
089: // @Override
090: // public void windowAdded(DockingWindow addedToWindow, DockingWindow addedWindow)
091: // {
092: // if (addedWindow.equals(view))
093: // ViewMenuItem.this.setSelected(true);
094: // }
095: //
096: // @Override
097: // public void windowRemoved(DockingWindow removedFromWindow, DockingWindow removedWindow)
098: // {
099: // if (removedWindow.equals(view))
100: // ViewMenuItem.this.setSelected(false);
101: // }
102: // }
103: }
104:
105: private View commentsView = null;
106: final MainFrame frame;
107: private RootWindow rootWindow;
108: private View sourceView = null;
109: private View summaryView = null;
110: private TabWindow tabs = null;
111:
112: private View topView = null;
113: private Map<View, ViewMenuItem> viewMenuItems = null;
114:
115: /**
116: * @param frame
117: */
118: public DockLayout(MainFrame frame) {
119: this .frame = frame;
120: }
121:
122: /* (non-Javadoc)
123: * @see edu.umd.cs.findbugs.gui2.LayoutManager#createWindowMenu()
124: */
125: public JMenu createWindowMenu() {
126:
127: viewMenuItems = new HashMap<View, ViewMenuItem>();
128: viewMenuItems.put(summaryView, new ViewMenuItem(summaryView,
129: "Bug summary"));
130: viewMenuItems.put(commentsView, new ViewMenuItem(commentsView,
131: "Comments"));
132: viewMenuItems.put(sourceView, new ViewMenuItem(sourceView,
133: "Source code"));
134:
135: JMenu windowMenu = new JMenu("Window");
136: windowMenu.setMnemonic(KeyEvent.VK_W);
137: windowMenu.add(viewMenuItems.get(summaryView));
138: windowMenu.add(viewMenuItems.get(commentsView));
139: windowMenu.add(viewMenuItems.get(sourceView));
140: return windowMenu;
141: }
142:
143: /* (non-Javadoc)
144: * @see edu.umd.cs.findbugs.gui2.LayoutManager#initialize()
145: */
146: public void initialize() {
147: ViewMap viewMap = new ViewMap();
148: topView = new View(L10N.getLocalString("view.bugs", "Bugs"),
149: null, frame.bugListPanel());
150: topView.getWindowProperties().setCloseEnabled(false);
151: viewMap.addView(0, topView);
152: summaryView = new View(L10N.getLocalString("view.bug_summary",
153: "Bug Summary"), null, frame.summaryTab());
154: viewMap.addView(1, summaryView);
155: commentsView = new View(L10N.getLocalString("view.comments",
156: "Comments"), null, frame.commentsPanel());
157: viewMap.addView(2, commentsView);
158: sourceView = new View(L10N.getLocalString("view.source",
159: "Source"), null, frame.createSourceCodePanel());
160: viewMap.addView(3, sourceView);
161:
162: rootWindow = DockingUtil.createRootWindow(viewMap, true);
163:
164: tabs = new TabWindow(new DockingWindow[] { summaryView,
165: commentsView, sourceView });
166: tabs.addListener(new DockParentListener());
167: tabs.setSelectedTab(0);
168: // tabs.getWindowProperties().setCloseEnabled(false);
169:
170: rootWindow
171: .setWindow(new SplitWindow(false, 0.4f, topView, tabs));
172:
173: DockingWindowsTheme theme = new ShapedGradientDockingTheme();
174: rootWindow.getRootWindowProperties().addSuperObject(
175: theme.getRootWindowProperties());
176:
177: try {
178: rootWindow.read(new ObjectInputStream(
179: new ByteArrayInputStream(GUISaveState.getInstance()
180: .getDockingLayout())), true);
181: } catch (IOException e) {
182: }
183:
184: DockingWindowListener listener = new DockingWindowAdapter() {
185: @Override
186: public void windowAdded(DockingWindow addedToWindow,
187: DockingWindow addedWindow) {
188: viewMenuItems.get(addedWindow).setSelected(true);
189:
190: addedToWindow.addListener(new DockParentListener());
191: }
192:
193: @Override
194: public void windowClosed(DockingWindow window) {
195: viewMenuItems.get(window).setSelected(false);
196: }
197: };
198:
199: summaryView.addListener(listener);
200: commentsView.addListener(listener);
201: sourceView.addListener(listener);
202:
203: frame.setLayout(new BorderLayout());
204: frame.add(rootWindow, BorderLayout.CENTER);
205: frame.add(frame.statusBar(), BorderLayout.SOUTH);
206: }
207:
208: /* (non-Javadoc)
209: * @see edu.umd.cs.findbugs.gui2.LayoutManager#makeCommentsVisible()
210: */
211: public void makeCommentsVisible() {
212: commentsView.makeVisible();
213:
214: }
215:
216: /* (non-Javadoc)
217: * @see edu.umd.cs.findbugs.gui2.LayoutManager#makeSourceVisible()
218: */
219: public void makeSourceVisible() {
220: sourceView.makeVisible();
221:
222: }
223:
224: /* (non-Javadoc)
225: * @see edu.umd.cs.findbugs.gui2.LayoutManager#saveState()
226: */
227: public void saveState() {
228: try {
229: // FIXME this is writing the wrong array and I don't know why
230: ByteArrayOutputStream dockingLayout = new ByteArrayOutputStream();
231: ObjectOutputStream out = new ObjectOutputStream(
232: dockingLayout);
233: rootWindow.write(out, true);
234: out.close();
235: GUISaveState.getInstance().setDockingLayout(
236: dockingLayout.toByteArray());
237: } catch (IOException e) {
238: }
239:
240: }
241:
242: /* (non-Javadoc)
243: * @see edu.umd.cs.findbugs.gui2.LayoutManager#setSourceTitle(java.lang.String)
244: */
245: public void setSourceTitle(final String title) {
246: sourceView.getWindowProperties().setTitleProvider(
247: new DockingWindowTitleProvider() {
248: public String getTitle(DockingWindow arg0) {
249: return title;
250: }
251: });
252: }
253:
254: }
|