Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
MatrixDialog.java
1 
16 package net.squiz.matrix.ui;
17 
18 import net.squiz.matrix.plaf.MatrixLookAndFeel;
19 import net.squiz.matrix.matrixtree.*;
20 import net.squiz.matrix.core.*;
21 import javax.swing.*;
22 import javax.swing.border.*;
23 import java.awt.event.*;
24 import java.awt.*;
25 import java.util.*;
26 
30 public class MatrixDialog extends JDialog {
31 
32  private int x= -1;
33  private int y= -1;
34  private static HashMap dialogs = new HashMap();
35  public Cursor HAND_CURSOR = new Cursor(Cursor.HAND_CURSOR);
36  public Cursor DEFAULT_CURSOR = new Cursor(Cursor.DEFAULT_CURSOR);
37 
38  public MatrixDialog() {
39  setUndecorated(true);
40  setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
41  }
42 
49  public static MatrixDialog getDialog(Class cls) {
50  return (MatrixDialog) dialogs.get(cls);
51  }
52 
57  public static void putDialog(MatrixDialog dialog) {
58  dialogs.put(dialog.getClass(), dialog);
59  }
60 
65  public static boolean hasDialog(Class cls) {
66  return dialogs.containsKey(cls);
67  }
68 
72  public void dispose() {
73  dialogs.remove(getClass());
74  super.dispose();
75  }
76 
77 
78  Point prevLoc;
79 
80  public Point getPrevLoc() {
81  return prevLoc;
82  }
83 
84  private void setPrevLoc(Point prevLoc) {
85  this.prevLoc = prevLoc;
86  }
87 
88 
93  public void centerDialogOnTree(Point locationOnScreen, Dimension treeDimension, Point prevLoc) {
94  setPrevLoc(prevLoc);
95 
96  int locX = (int)locationOnScreen.getX()+(int)((treeDimension.getWidth()/2)-(getWidth()/2));
97  if (locX < 0) {
98  locX = 5;
99  }
100 
101  setLocation(locX,(int)(locationOnScreen.getY()+(treeDimension.getHeight()/3)), false);
102  }
103 
104  public JPanel getTopPanel(String dialogTitle) {
105  JPanel topPanel = new JPanel();
106  topPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
107  topPanel.setBackground(MatrixLookAndFeel.PANEL_COLOUR);
108 
109  JLabel title = new JLabel(dialogTitle);
110  title.setFont(MatrixTreeBus.getActiveTree().getFontInUse());
111  title.setHorizontalTextPosition(SwingConstants.CENTER);
112  title.setOpaque(false);
113  title.setForeground(Color.black);
114 
115  topPanel.add(title);
116  enableDrag(topPanel);
117 
118  return topPanel;
119  }
120 
121  public void closeOnClick(final JLabel component, final String img_prefix) {
122  component.addMouseListener(new MouseAdapter(){
123  public void mouseClicked(MouseEvent e){
124  setCursor(DEFAULT_CURSOR);
125  dispose();
126  }
127 
128  public void mouseExited(MouseEvent e) {
129  component.setIcon(GUIUtilities.getAssetMapIcon(img_prefix + ".png"));
130  setCursor(DEFAULT_CURSOR);
131  }
132 
133  public void mouseEntered(MouseEvent e) {
134  setCursor(HAND_CURSOR);
135  component.setIcon(GUIUtilities.getAssetMapIcon(img_prefix + "_on.png"));
136  }
137  });
138  }
139 
140  public void enableDrag(final JComponent component) {
141  component.addMouseListener(new MouseAdapter(){
142  public void mouseReleased(MouseEvent e){
143  x = e.getX();
144  y = e.getY();
145  }
146  public void mousePressed(MouseEvent e){
147  x = e.getX();
148  y = e.getY();
149  }
150  });
151 
152  component.addMouseMotionListener(new MouseMotionAdapter(){
153  public void mouseDragged(MouseEvent e){
154  int posX = getLocationOnScreen().x+(e.getX()-x);
155  int posY = getLocationOnScreen().y+(e.getY()-y);
156  setLocation(posX, posY, true);
157  setPrevLoc(new Point(posX, posY));
158  }
159  });
160  }
161 
162 
163 
164  public void setLocation(int x, int y, boolean force) {
165  if (force || (getPrevLoc() == null)) {
166  super.setLocation(x, y);
167  } else {
168  x = (int)getPrevLoc().getX();
169  y = (int)getPrevLoc().getY();
170  super.setLocation(x, y);
171  }
172  setPrevLoc(new Point(x, y));
173  }
174 }