Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / LayoutKeyEvent.java @ 24962

History | View | Annotate | Download (5.01 KB)

1
package com.iver.cit.gvsig.project.documents.layout;
2

    
3
import java.awt.KeyEventDispatcher;
4
import java.awt.event.KeyEvent;
5
import java.awt.geom.Rectangle2D;
6

    
7
import org.gvsig.tools.undo.RedoException;
8
import org.gvsig.tools.undo.UndoException;
9

    
10
import com.iver.andami.PluginServices;
11
import com.iver.andami.ui.mdiManager.IWindow;
12
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
13
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
14

    
15
public class LayoutKeyEvent implements KeyEventDispatcher{
16
        private static IFFrame[] selectFFrames=new IFFrame[0];
17
    private int difX;
18
    private int difY;
19

    
20
        public static boolean copy(Layout layout) {
21
                IFFrame[] fframes = layout.getLayoutContext().getFFrameSelected();
22
                if (fframes.length==0)
23
                        return false;
24
                selectFFrames = new IFFrame[fframes.length];
25
                for (int i = 0; i < fframes.length; i++) {
26
                        selectFFrames[i] = fframes[i].cloneFFrame(layout);
27
                }
28
                return true;
29
        }
30
        public static boolean cut(Layout layout) {
31
                IFFrame[] fframes = layout.getLayoutContext().getFFrameSelected();
32
                if (fframes.length==0)
33
                        return false;
34
                selectFFrames = new IFFrame[fframes.length];
35
                for (int i = 0; i < fframes.length; i++) {
36
                        selectFFrames[i] = fframes[i].cloneFFrame(layout);
37
                }
38
                layout.getLayoutContext().delFFrameSelected();
39
                layout.getLayoutContext().callLayoutDrawListeners();
40
                return true;
41
        }
42
        public static boolean paste(Layout layout) {
43
                IFFrame copyFFrame = null;
44
                layout.getLayoutContext().getFrameCommandsRecord().startComplex(PluginServices.getText(layout, "paste_elements"));
45
                for (int i = 0; i < selectFFrames.length; i++) {
46
                        copyFFrame = selectFFrames[i].cloneFFrame(layout);
47
                        if (i == 0)
48
                                layout.getLayoutContext().addFFrame(copyFFrame, true, true);
49
                        else
50
                                layout.getLayoutContext().addFFrame(copyFFrame, false, true);
51
                }
52
                layout.getLayoutContext().getFrameCommandsRecord().endComplex();
53
                layout.getLayoutContext().callLayoutDrawListeners();
54
                return true;
55
        }
56
        public static boolean undo(Layout layout) {
57
                LayoutContext lcontext=layout.getLayoutContext();
58
                try {
59
                        lcontext.getFrameCommandsRecord().undo();
60
                } catch (UndoException e) {
61
                        e.printStackTrace();
62
                }
63
                layout.getLayoutContext().updateFFrames();
64
                layout.getLayoutContext().callLayoutDrawListeners();
65
                PluginServices.getMainFrame().enableControls();
66
                return true;
67
        }
68
        public static boolean redo(Layout layout) {
69
                LayoutContext lcontext=layout.getLayoutContext();
70
                try {
71
                        lcontext.getFrameCommandsRecord().redo();
72
                } catch (RedoException e) {
73
                        e.printStackTrace();
74
                }
75
                lcontext.updateFFrames();
76
                lcontext.callLayoutDrawListeners();
77
                PluginServices.getMainFrame().enableControls();
78
                return true;
79
        }
80

    
81
    public boolean dispatchKeyEvent(KeyEvent e) {
82
                IWindow view = PluginServices.getMDIManager().getActiveWindow();
83

    
84
                if (e.getID() == KeyEvent.KEY_PRESSED || !(view instanceof Layout) || !(e.getSource() instanceof LayoutControl))
85
                        return false;
86
                Layout layout=(Layout)view;
87
                if (!layout.getLayoutContext().isEditable())
88
                        return false;
89
                IFFrame[] fframes = layout.getLayoutContext().getFFrameSelected();
90
                int dif = 10;
91
                if (e.getModifiers() == KeyEvent.CTRL_MASK) {
92
                        switch (e.getKeyCode()) {
93
                        case KeyEvent.VK_C:
94
                                copy(layout);
95
                                break;
96
                        case KeyEvent.VK_X:
97
                                cut(layout);
98
                                break;
99
                        case KeyEvent.VK_V:
100
                                paste(layout);
101
                                break;
102
                        case KeyEvent.VK_Z:
103
                                undo(layout);
104
                                break;
105
                        case KeyEvent.VK_Y:
106
                                redo(layout);
107
                                break;
108
                        }
109
                } else {
110
                        switch (e.getKeyCode()) {
111
                        case KeyEvent.VK_LEFT:
112
                                difX = difX - dif;
113
                                break;
114

    
115
                        case KeyEvent.VK_RIGHT:
116
                                difX = difX + dif;
117
                                break;
118
                        case KeyEvent.VK_UP:
119
                                difY = difY - dif;
120
                                break;
121
                        case KeyEvent.VK_DOWN:
122
                                difY = difY + dif;
123
                                break;
124
                        case KeyEvent.VK_DELETE:
125
                        case KeyEvent.VK_BACK_SPACE:
126
                                layout.getLayoutContext().delFFrameSelected();
127
                                layout.getLayoutControl().refresh();
128
                                break;
129
                        }
130
                        if (e.getKeyCode()==KeyEvent.VK_LEFT || e.getKeyCode()==KeyEvent.VK_RIGHT || e.getKeyCode()==KeyEvent.VK_UP || e.getKeyCode()==KeyEvent.VK_DOWN) {
131
                                if (fframes.length==0)
132
                                        return false;
133
                                layout.getLayoutContext().getFrameCommandsRecord().startComplex(PluginServices.getText(this, "move_elements"));
134
                        for (int i = 0; i < fframes.length; i++) {
135
                                IFFrame fframeAux = fframes[i].cloneFFrame(layout);
136
                                Rectangle2D r = getRectMove(fframes[i]
137
                                                .getBoundingBox(layout.getLayoutControl().getAT()), difX, difY);
138
                                fframeAux.setBoundBox(FLayoutUtilities.toSheetRect(r,
139
                                                layout.getLayoutControl().getAT()));
140
                                layout.getLayoutContext().getFrameCommandsRecord().update(fframes[i], fframeAux);
141
                        }
142
                        layout.getLayoutContext().getFrameCommandsRecord().endComplex();
143
                        layout.getLayoutContext().updateFFrames();
144
                        layout.getLayoutControl().refresh();
145
                        }
146
                        difX = 0;
147
                        difY = 0;
148

    
149
                }
150
                return true;
151
        }
152
        private Rectangle2D getRectMove(Rectangle2D r,int difX,int difY) {
153
                Rectangle2D rectMove=new Rectangle2D.Double();
154
                rectMove.setRect(r.getX()+difX,r.getY()+difY,r.getWidth(),r.getHeight());
155
                return rectMove;
156
        }
157
        public static boolean hasSelection() {
158
                return selectFFrames.length>0;
159
        }
160

    
161
}