1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/*
* Created on 8 juin 2005
*/
package ponctuel.client.graphic.util;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.border.BevelBorder;
import javax.swing.event.MouseInputListener;
//import sun.awt.VerticalBagLayout;
/**
* @author komando
* TODO regler le probleme du layout
* TODO penser a l'aspect esthetique
* TODO synchroniser le checkBox du menu avec le bouton fermer
*/
public class CustomPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
//constants
private static int HGAP = 5;
private static int VGAP = 0;
// private static int HEIGHT = 36;
//get to be changed by icons
private static String closeStr = "x";
private static String expStr = "+";
private static String cloStr = "-";
//swing containers
private JPanel northPane = new JPanel(new BorderLayout(HGAP,VGAP));
private JPanel centerPane = new CenterPane();
private JPanel northEastPane = new JPanel();
private JPanel northCenterPane = new JPanel();
//swing components
private JLabel mainLabel = new JLabel();
private JButton mainButton = new JButton();
private JButton closeButton = new JButton();
private JButton expandButton = new JButton();
//other vars
private boolean isClosed;
private String title = "";
/**
* Contructor
* @param title of the Pane
*/
public CustomPanel(String title){
super(new BorderLayout());
this.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
this.title=title;
this.constructPane();
}
/**
* add panes to the main one
*/
private void constructPane(){
add(northPane, BorderLayout.NORTH);
add(centerPane, BorderLayout.CENTER);
setActions();
openNorthPane();
isClosed=false;
}
/**
* close main Pane
*/
private void closePane(){
centerPane.setVisible(false);
closeNorthPane();
repaint();
isClosed=true;
}
/**
* open main Pane
*/
private void openPane(){
centerPane.setVisible(true);
openNorthPane();
repaint();
isClosed=false;
}
/**
* the closed northPane Behaviour
*/
private void closeNorthPane(){
northPane.removeAll();
northPane.add(northCenterPane,BorderLayout.CENTER);
northPane.add(northEastPane, BorderLayout.EAST);
//get to be changed
mainLabel.setText("::");
mainButton.setText(title);
expandButton.setText(expStr);
closeButton.setText(closeStr);
//addin' components
northCenterPane.add(mainLabel);
northCenterPane.add(mainButton);
northEastPane.add(expandButton);
northEastPane.add(closeButton);
}
/**
* the opened northPane behavior
*/
private void openNorthPane(){
northPane.removeAll();
northPane.add(mainLabel,BorderLayout.CENTER);
northPane.add(northEastPane, BorderLayout.EAST);
//get to be changed
mainLabel.setText(title);
expandButton.setText(cloStr);
closeButton.setText(closeStr);
//addin' components
northEastPane.add(expandButton);
northEastPane.add(closeButton);
}
/**
* adds the necessary actions to the components
*/
private void setActions(){
//main button
mainButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
openPane();
}
});
//expand button
expandButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (isClosed){
openPane();
} else
closePane();
}
});
//close button
closeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
}
/**
* adds comp to the center panel
* @param comp
*/
public void addCenter(final Component comp){
centerPane.add(comp);
}
}
/**
* this class is used to set up the draggin and resizin actions
*/
class CenterPane extends JPanel implements MouseInputListener{
/**
*
*/
private static final long serialVersionUID = 1L;
//constants
private static int BOUNDS = 48;
private static int HGAP = 0;
private static int VGAP = 5;
private int newY; //new location of pane
private Dimension dim;
private boolean isDrag=false;
private Cursor s;
private int type; // cursor type
private JPanel parent=null;
public CenterPane(){
super(new BorderLayout(HGAP,VGAP));
// setLayout(new BorderLayout(HGAP,VGAP));
setBorder(BorderFactory.createEtchedBorder(0));
addMouseListener(this);
addMouseMotionListener(this);
dim = new Dimension(getWidth(),getHeight());
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
if(isDrag)
changeCursor(Cursor.DEFAULT_CURSOR);
isDrag=false;
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {
changeCursor(Cursor.DEFAULT_CURSOR);
}
public void mouseDragged(MouseEvent e) {
isDrag=true;
newY = e.getY();
dim.height=newY;
setPreferredSize(dim);
parent = (JPanel) getParent();
parent.revalidate();
changeCursor(Cursor.S_RESIZE_CURSOR);
}
public void mouseMoved(MouseEvent e) {
if(e.getY() >= (getParent().getHeight()-BOUNDS))
type=Cursor.S_RESIZE_CURSOR;
else
type=Cursor.DEFAULT_CURSOR;
changeCursor(type);
}
private void changeCursor(int type){
s = Cursor.getPredefinedCursor(type);
Container c = getTopLevelAncestor();
if (c instanceof JFrame){
((JFrame)c).getGlassPane().setVisible(true);
((JFrame)c).getGlassPane().setCursor(s);
} else if (c instanceof JApplet){
((JApplet)c).getGlassPane().setVisible(true);
((JApplet)c).getGlassPane().setCursor(s);
} else if (c instanceof JWindow){
((JWindow)c).getGlassPane().setVisible(true);
((JWindow)c).getGlassPane().setCursor(s);
} else if (c instanceof JDialog){
((JDialog)c).getGlassPane().setVisible(true);
((JDialog)c).getGlassPane().setCursor(s);
}
}
} |
Partager