1 package org.antlr.works.swing; 2 3 import org.antlr.works.prefs.AWPrefs; 4 import org.antlr.works.utils.IconManager; 5 import org.antlr.xjlib.appkit.frame.XJDialog; 6 7 import javax.swing.*; 8 import java.awt.*; 9 import java.awt.event.ActionEvent ; 10 import java.awt.event.ActionListener ; 11 import java.beans.PropertyChangeEvent ; 12 import java.beans.PropertyChangeListener ; 13 43 44 public class DetachablePanel extends JPanel { 45 46 protected DetachablePanelDelegate delegate; 47 protected JPanel mainPanel; 48 protected TitlePanel titlePanel; 49 protected String title; 50 51 protected boolean detached = false; 52 protected JButton detach; 53 protected XJDialog window; 54 protected int tag; 55 56 public DetachablePanel(String title, DetachablePanelDelegate delegate) { 57 super(new BorderLayout()); 58 59 this.delegate = delegate; 60 this.title = title; 61 62 createTitleBar(title); 63 64 mainPanel = new JPanel(new BorderLayout()); 65 add(mainPanel, BorderLayout.CENTER); 66 67 KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); 68 focusManager.addPropertyChangeListener( 69 new PropertyChangeListener () { 70 public void propertyChange(PropertyChangeEvent e) { 71 String prop = e.getPropertyName(); 72 if(prop.equals("focusOwner") && e.getNewValue() != null) { 73 Component c = (Component)e.getNewValue(); 74 if(isParentOf(c)) 75 focusGained(); 76 else 77 focusLost(); 78 } 79 } 80 } 81 ); 82 } 83 84 public void createTitleBar(String title) { 85 Box box = Box.createHorizontalBox(); 86 JLabel l = new JLabel(title); 87 l.setFont(l.getFont().deriveFont(11.0f)); 88 l.setForeground(Color.white); 89 box.add(Box.createHorizontalStrut(2)); 90 box.add(l); 91 box.add(Box.createHorizontalGlue()); 92 box.add(detach = createDetachButton()); 93 94 titlePanel = new TitlePanel(); 95 titlePanel.setMinimumSize(new Dimension(0, 15)); 96 titlePanel.setMaximumSize(new Dimension(0, 15)); 97 titlePanel.setPreferredSize(new Dimension(0, 15)); 98 titlePanel.add(box); 99 100 super.add(titlePanel, BorderLayout.NORTH); 101 } 102 103 public JButton createDetachButton() { 104 JButton detach = new JButton(IconManager.shared().getIconDetach()); 105 detach.setBorder(null); 106 detach.setBorderPainted(false); 107 detach.setOpaque(false); 108 detach.setFocusable(false); 109 110 detach.addActionListener(new ActionListener () { 111 public void actionPerformed(ActionEvent e) { 112 if(detached) 113 attach(); 114 else 115 detach(); 116 } 117 }); 118 return detach; 119 } 120 121 public boolean isParentOf(Component c) { 122 Component p = c.getParent(); 123 return p != null && (p == this || isParentOf(p)); 124 } 125 126 public void focusGained() { 127 titlePanel.setFocused(true); 128 } 129 130 public void focusLost() { 131 titlePanel.setFocused(false); 132 } 133 134 public void setTag(int tag) { 135 this.tag = tag; 136 } 137 138 public int getTag() { 139 return tag; 140 } 141 142 public void setVisible(boolean flag) { 143 super.setVisible(flag); 144 if(detached) 145 window.setVisible(flag); 146 } 147 148 public boolean isDetached() { 149 return detached; 150 } 151 152 private Point previousPosition; 153 private Dimension previousSize; 154 155 public void detach() { 156 detached = true; 157 158 Point p = getLocationOnScreen(); 159 160 delegate.panelDoDetach(this); 161 162 detach.setIcon(IconManager.shared().getIconAttach()); 163 164 if(AWPrefs.getDetachableChildren()) 165 window = new DetachableWindow(delegate.panelParentContainer()); 166 else 167 window = new DetachableWindow(null); 168 window.setTitle(title); 169 if(previousPosition == null) { 170 window.setPosition(p.x, p.y); 171 } else { 172 window.setPosition(previousPosition); 173 } 174 175 if(previousSize == null) { 176 window.setSize(getWidth(), getHeight()); 177 } else { 178 window.setSize(previousSize); 179 } 180 181 window.getContentPane().add(this); 182 183 window.setVisible(true); 184 } 185 186 public void attach() { 187 detached = false; 188 previousPosition = window.getPosition(); 189 previousSize = window.getSize(); 190 window.getContentPane().remove(0); 191 window.close(); 192 detach.setIcon(IconManager.shared().getIconDetach()); 193 delegate.panelDoAttach(this); 194 } 195 196 public class TitlePanel extends JPanel { 197 198 public boolean focused = false; 199 200 public TitlePanel() { 201 super(new BorderLayout()); 202 } 203 204 public void setFocused(boolean flag) { 205 this.focused = flag; 206 repaint(); 207 } 208 209 public void paintComponent(Graphics g) { 210 Color startColor; 211 Color endColor; 212 if(focused) { 213 startColor = new Color(0.1f, 0.6f, 0.9f); 214 endColor = new Color(0.8f, 0.9f, 1.0f); 215 } else { 216 startColor = new Color(0.7f, 0.7f, 0.7f); 217 endColor = new Color(0.9f, 0.9f, 0.9f); 218 } 219 220 int x = getVisibleRect().width; 221 int y = getVisibleRect().height; 222 223 GradientPaint gradient = new GradientPaint(0, 0, startColor, x, y, endColor); 224 225 Graphics2D g2d = (Graphics2D)g; 226 g2d.setPaint(gradient); 227 g2d.fillRect(0, 0, x, y); 228 } 229 } 230 231 public class DetachableWindow extends XJDialog { 232 233 public DetachableWindow(Container container) { 234 super(container, false); 235 } 236 237 public void dialogWillCloseCancel() { 238 if(detached) { 239 DetachablePanel.this.setVisible(false); 240 delegate.panelDoClose(DetachablePanel.this); 241 } 242 } 243 } 244 } 245 | Popular Tags |