001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.ui;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Component;
017: import java.awt.event.MouseAdapter;
018: import java.awt.event.MouseEvent;
019: import java.beans.PropertyChangeEvent;
020: import java.beans.PropertyChangeListener;
021:
022: import javax.swing.DefaultListModel;
023: import javax.swing.Icon;
024: import javax.swing.JLabel;
025: import javax.swing.JList;
026: import javax.swing.JPanel;
027: import javax.swing.ListCellRenderer;
028:
029: import com.eviware.soapui.ui.desktop.DesktopListener;
030: import com.eviware.soapui.ui.desktop.DesktopPanel;
031: import com.eviware.soapui.ui.desktop.SoapUIDesktop;
032:
033: /**
034: * List for displaying current DesktopPanels
035: *
036: * @author Ole.Matzura
037: */
038:
039: public class JDesktopPanelsList extends JPanel {
040: private DefaultListModel desktopPanels;
041: private JList desktopPanelsList;
042: private SoapUIDesktop desktop;
043: private DesktopPanelPropertyChangeListener desktopPanelPropertyListener = new DesktopPanelPropertyChangeListener();
044: private InternalDesktopListener desktopListener = new InternalDesktopListener();
045:
046: public JDesktopPanelsList(SoapUIDesktop desktop) {
047: super (new BorderLayout());
048: setDesktop(desktop);
049:
050: desktopPanels = new DefaultListModel();
051: desktopPanelsList = new JList(desktopPanels);
052: desktopPanelsList
053: .setCellRenderer(new DesktopItemsCellRenderer());
054: desktopPanelsList.setToolTipText("Open windows");
055: desktopPanelsList.addMouseListener(new MouseAdapter() {
056:
057: public void mouseClicked(MouseEvent e) {
058: if (e.getClickCount() < 2)
059: return;
060:
061: JDesktopPanelsList.this .desktop
062: .showDesktopPanel((DesktopPanel) desktopPanelsList
063: .getSelectedValue());
064: }
065: });
066:
067: add(desktopPanelsList, BorderLayout.CENTER);
068: }
069:
070: private class DesktopPanelPropertyChangeListener implements
071: PropertyChangeListener {
072: public void propertyChange(PropertyChangeEvent evt) {
073: DesktopPanel desktopPanel = (DesktopPanel) evt.getSource();
074: int ix = desktopPanels.indexOf(desktopPanel);
075: if (ix >= 0)
076: desktopPanels.set(ix, desktopPanel);
077: }
078: }
079:
080: private static class DesktopItemsCellRenderer extends JLabel
081: implements ListCellRenderer {
082: public Component getListCellRendererComponent(JList list,
083: Object value, int index, boolean isSelected,
084: boolean cellHasFocus) {
085: DesktopPanel desktopPanel = (DesktopPanel) value;
086:
087: String title = desktopPanel.getTitle();
088: setText(title);
089: setToolTipText(desktopPanel.getDescription());
090: Icon icon = desktopPanel.getIcon();
091: if (icon != null)
092: setIcon(icon);
093:
094: if (isSelected) {
095: setBackground(list.getSelectionBackground());
096: setForeground(list.getSelectionForeground());
097: } else {
098: setBackground(list.getBackground());
099: setForeground(list.getForeground());
100: }
101:
102: setEnabled(list.isEnabled());
103: setFont(list.getFont());
104: setOpaque(true);
105:
106: return this ;
107: }
108: }
109:
110: private class InternalDesktopListener implements DesktopListener {
111: public void desktopPanelSelected(DesktopPanel desktopPanel) {
112: desktopPanelsList.setSelectedValue(desktopPanel, false);
113: }
114:
115: public void desktopPanelCreated(DesktopPanel desktopPanel) {
116: desktopPanels.addElement(desktopPanel);
117: desktopPanelsList.setSelectedValue(desktopPanel, false);
118:
119: desktopPanel
120: .addPropertyChangeListener(desktopPanelPropertyListener);
121: }
122:
123: public void desktopPanelClosed(DesktopPanel desktopPanel) {
124: desktopPanels.removeElement(desktopPanel);
125: desktopPanel
126: .removePropertyChangeListener(desktopPanelPropertyListener);
127: }
128: }
129:
130: public void setDesktop(SoapUIDesktop newDesktop) {
131: if (desktop != null) {
132: desktop.removeDesktopListener(desktopListener);
133:
134: while (desktopPanels.size() > 0) {
135: DesktopPanel desktopPanel = (DesktopPanel) desktopPanels
136: .getElementAt(0);
137: desktopPanel
138: .removePropertyChangeListener(desktopPanelPropertyListener);
139: desktopPanels.remove(0);
140: }
141:
142: }
143:
144: desktop = newDesktop;
145:
146: desktop.addDesktopListener(desktopListener);
147:
148: for (DesktopPanel desktopPanel : desktop.getDesktopPanels()) {
149: desktopPanel
150: .addPropertyChangeListener(desktopPanelPropertyListener);
151: desktopPanels.addElement(desktopPanel);
152: }
153: }
154: }
|