001package com.ganteater.ae.desktop.editor; 002 003import java.awt.BorderLayout; 004import java.awt.Container; 005import java.awt.Cursor; 006import java.awt.Dimension; 007import java.awt.Frame; 008import java.awt.Rectangle; 009import java.awt.event.MouseEvent; 010import java.awt.event.MouseListener; 011import java.awt.event.MouseMotionListener; 012import java.util.HashSet; 013 014import javax.swing.JDialog; 015import javax.swing.JLabel; 016import javax.swing.JPanel; 017 018import com.ganteater.ae.desktop.util.UIUtils; 019 020/** 021 * This JDialog subclass supports resizing and undecorated mode. There is also 022 * support for disalbing the resizing on any given side. 023 */ 024public class CUndecoratedResizeableDialog extends JDialog implements MouseListener, MouseMotionListener { 025 private static final long serialVersionUID = 1L; 026 027 static final protected int NW_SIDE = 1; 028 static final protected int N_SIDE = 2; 029 static final protected int NE_SIDE = 3; 030 static final protected int L_SIDE = 4; 031 static final protected int R_SIDE = 5; 032 static final protected int SW_SIDE = 6; 033 static final protected int S_SIDE = 7; 034 static final protected int SE_SIDE = 8; 035 036 private JPanel resizePanel = null; 037 private JPanel contentPanel = null; 038 private JLabel left = null; 039 private JLabel right = null; 040 private JLabel top = null; 041 private JLabel bottom = null; 042 private JLabel topleft = null; 043 private JLabel topright = null; 044 private JLabel bottomleft = null; 045 private JLabel bottomright = null; 046 047 private Rectangle startSize = null; 048 049 private HashSet<Integer> hsDisabledSides = new HashSet<Integer>(); 050 051 public CUndecoratedResizeableDialog(Frame owner) { 052 super(owner); 053 initialize(); 054 } 055 056 public CUndecoratedResizeableDialog() { 057 super(); 058 initialize(); 059 } 060 061 private void initialize() { 062 resizePanel = new JPanel(new BorderLayout()); 063 contentPanel = new JPanel(new BorderLayout()); 064 setUndecorated(true); 065 066 left = new JLabel(); 067 right = new JLabel(); 068 top = new JLabel(); 069 bottom = new JLabel(); 070 topleft = new JLabel(); 071 topright = new JLabel(); 072 bottomleft = new JLabel(); 073 bottomright = new JLabel(); 074 075 left.setPreferredSize(new Dimension(2, 0)); 076 left.setMinimumSize(new Dimension(2, 0)); 077 078 right.setPreferredSize(new Dimension(2, 0)); 079 right.setMinimumSize(new Dimension(2, 0)); 080 top.setPreferredSize(new Dimension(0, 2)); 081 top.setMinimumSize(new Dimension(0, 2)); 082 bottom.setPreferredSize(new Dimension(0, 2)); 083 bottom.setMinimumSize(new Dimension(0, 2)); 084 085 left.addMouseListener(this); 086 right.addMouseListener(this); 087 top.addMouseListener(this); 088 bottom.addMouseListener(this); 089 090 left.addMouseMotionListener(this); 091 right.addMouseMotionListener(this); 092 top.addMouseMotionListener(this); 093 bottom.addMouseMotionListener(this); 094 095 left.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR)); 096 right.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR)); 097 top.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR)); 098 bottom.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR)); 099 100 topleft.setPreferredSize(new Dimension(4, 4)); 101 topleft.setMinimumSize(new Dimension(4, 4)); 102 topright.setPreferredSize(new Dimension(4, 4)); 103 topright.setMinimumSize(new Dimension(4, 4)); 104 bottomleft.setPreferredSize(new Dimension(4, 4)); 105 bottomleft.setMinimumSize(new Dimension(4, 4)); 106 bottomright.setPreferredSize(new Dimension(4, 4)); 107 bottomright.setMinimumSize(new Dimension(4, 4)); 108 109 topleft.addMouseListener(this); 110 topright.addMouseListener(this); 111 bottomleft.addMouseListener(this); 112 bottomright.addMouseListener(this); 113 114 topleft.addMouseMotionListener(this); 115 topright.addMouseMotionListener(this); 116 bottomleft.addMouseMotionListener(this); 117 bottomright.addMouseMotionListener(this); 118 119 topleft.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR)); 120 topright.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR)); 121 bottomleft.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR)); 122 bottomright.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR)); 123 124 JPanel northPanel = new JPanel(new BorderLayout()); 125 northPanel.add(topleft, BorderLayout.WEST); 126 northPanel.add(top, BorderLayout.CENTER); 127 northPanel.add(topright, BorderLayout.EAST); 128 129 JPanel southPanel = new JPanel(new BorderLayout()); 130 southPanel.add(bottomleft, BorderLayout.WEST); 131 southPanel.add(bottom, BorderLayout.CENTER); 132 southPanel.add(bottomright, BorderLayout.EAST); 133 134 resizePanel.add(left, BorderLayout.WEST); 135 resizePanel.add(right, BorderLayout.EAST); 136 resizePanel.add(northPanel, BorderLayout.NORTH); 137 resizePanel.add(southPanel, BorderLayout.SOUTH); 138 resizePanel.add(contentPanel, BorderLayout.CENTER); 139 140 this.setContentPane(resizePanel); 141 } 142 143 @Override 144 public Container getContentPane() { 145 return contentPanel; 146 } 147 148 public void mousePressed(MouseEvent e) { 149 startSize = this.getBounds(); 150 } 151 152 public void mouseDragged(MouseEvent e) { 153 if (startSize == null) 154 return; 155 if (e.getSource() == topleft) { 156 if (hsDisabledSides.contains(NW_SIDE)) 157 return; 158 startSize.y += e.getY(); 159 startSize.height -= e.getY(); 160 startSize.x += e.getX(); 161 startSize.width -= e.getX(); 162 this.setBounds(startSize); 163 } else if (e.getSource() == top) { 164 if (hsDisabledSides.contains(N_SIDE)) 165 return; 166 startSize.y += e.getY(); 167 startSize.height -= e.getY(); 168 this.setBounds(startSize); 169 } else if (e.getSource() == topright) { 170 if (hsDisabledSides.contains(NE_SIDE)) 171 return; 172 startSize.y += e.getY(); 173 startSize.height -= e.getY(); 174 startSize.width += e.getX(); 175 this.setBounds(startSize); 176 177 } else if (e.getSource() == left) { 178 if (hsDisabledSides.contains(L_SIDE)) 179 return; 180 startSize.x += e.getX(); 181 startSize.width -= e.getX(); 182 this.setBounds(startSize); 183 } else if (e.getSource() == right) { 184 if (hsDisabledSides.contains(R_SIDE)) 185 return; 186 startSize.width += e.getX(); 187 this.setBounds(startSize); 188 } else if (e.getSource() == bottomleft) { 189 if (hsDisabledSides.contains(SW_SIDE)) 190 return; 191 startSize.height += e.getY(); 192 startSize.x += e.getX(); 193 startSize.width -= e.getX(); 194 this.setBounds(startSize); 195 } else if (e.getSource() == bottom) { 196 if (hsDisabledSides.contains(S_SIDE)) 197 return; 198 startSize.height += e.getY(); 199 this.setBounds(startSize); 200 } else if (e.getSource() == bottomright) { 201 if (hsDisabledSides.contains(SE_SIDE)) 202 return; 203 startSize.height += e.getY(); 204 startSize.width += e.getX(); 205 this.setBounds(startSize); 206 } 207 UIUtils.saveDialogPreferedView(this, getClass().getName()); 208 } 209 210 public void mouseClicked(MouseEvent e) { 211 } 212 213 public void mouseMoved(MouseEvent e) { 214 } 215 216 public void mouseReleased(MouseEvent e) { 217 } 218 219 public void mouseEntered(MouseEvent e) { 220 } 221 222 public void mouseExited(MouseEvent e) { 223 } 224 225 protected void disableResizeSide(int side) { 226 hsDisabledSides.add(side); 227 if (side == NW_SIDE) { 228 topleft.setCursor(Cursor.getDefaultCursor()); 229 } else if (side == N_SIDE) { 230 top.setCursor(Cursor.getDefaultCursor()); 231 } else if (side == NE_SIDE) { 232 topright.setCursor(Cursor.getDefaultCursor()); 233 } else if (side == L_SIDE) { 234 left.setCursor(Cursor.getDefaultCursor()); 235 } else if (side == R_SIDE) { 236 right.setCursor(Cursor.getDefaultCursor()); 237 } else if (side == SW_SIDE) { 238 bottomleft.setCursor(Cursor.getDefaultCursor()); 239 } else if (side == S_SIDE) { 240 bottom.setCursor(Cursor.getDefaultCursor()); 241 } else if (side == SE_SIDE) { 242 bottomright.setCursor(Cursor.getDefaultCursor()); 243 } 244 } 245 246 public void showDialog() { 247 UIUtils.applyPreferedView(this, getClass().getName()); 248 setVisible(true); 249 } 250}