Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / LayoutKeyEvent.java @ 23074

History | View | Annotate | Download (4.91 KB)

1 7304 caballero
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 com.iver.andami.PluginServices;
8
import com.iver.andami.ui.mdiManager.IWindow;
9 10626 caballero
import com.iver.cit.gvsig.exceptions.commands.EditionCommandException;
10 7304 caballero
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
11 9392 caballero
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
12 7304 caballero
13
public class LayoutKeyEvent implements KeyEventDispatcher{
14 9807 caballero
        private static IFFrame[] selectFFrames=new IFFrame[0];
15 7304 caballero
    private int difX;
16
    private int difY;
17
18 9807 caballero
        public static boolean copy(Layout layout) {
19
                IFFrame[] fframes = layout.getLayoutContext().getFFrameSelected();
20
                if (fframes.length==0)
21
                        return false;
22
                selectFFrames = new IFFrame[fframes.length];
23
                for (int i = 0; i < fframes.length; i++) {
24
                        selectFFrames[i] = fframes[i].cloneFFrame(layout);
25
                }
26
                return true;
27
        }
28
        public static boolean cut(Layout layout) {
29
                IFFrame[] fframes = layout.getLayoutContext().getFFrameSelected();
30
                if (fframes.length==0)
31
                        return false;
32
                selectFFrames = new IFFrame[fframes.length];
33
                for (int i = 0; i < fframes.length; i++) {
34
                        selectFFrames[i] = fframes[i].cloneFFrame(layout);
35
                }
36
                layout.getLayoutContext().delFFrameSelected();
37
                layout.getLayoutContext().callLayoutDrawListeners();
38
                return true;
39
        }
40
        public static boolean paste(Layout layout) {
41
                IFFrame copyFFrame = null;
42
                layout.getLayoutContext().getEFS().startComplexCommand();
43
                for (int i = 0; i < selectFFrames.length; i++) {
44
                        copyFFrame = selectFFrames[i].cloneFFrame(layout);
45
                        if (i == 0)
46
                                layout.getLayoutContext().addFFrame(copyFFrame, true, true);
47
                        else
48
                                layout.getLayoutContext().addFFrame(copyFFrame, false, true);
49
                }
50
                layout.getLayoutContext().getEFS().endComplexCommand(
51
                                PluginServices.getText(layout, "paste_elements"));
52
                layout.getLayoutContext().callLayoutDrawListeners();
53
                return true;
54
        }
55
        public static boolean undo(Layout layout) {
56 10626 caballero
                try {
57
                        layout.getLayoutContext().getEFS().undo();
58
                } catch (EditionCommandException e) {
59
                        e.printStackTrace();
60
                }
61 9807 caballero
                layout.getLayoutContext().updateFFrames();
62
                layout.getLayoutContext().callLayoutDrawListeners();
63
                PluginServices.getMainFrame().enableControls();
64
                return true;
65
        }
66
        public static boolean redo(Layout layout) {
67 10626 caballero
                try {
68
                        layout.getLayoutContext().getEFS().redo();
69
                } catch (EditionCommandException e) {
70
                        e.printStackTrace();
71
                }
72 9807 caballero
                layout.getLayoutContext().updateFFrames();
73
                layout.getLayoutContext().callLayoutDrawListeners();
74
                PluginServices.getMainFrame().enableControls();
75
                return true;
76
        }
77
78
    public boolean dispatchKeyEvent(KeyEvent e) {
79 7304 caballero
                IWindow view = PluginServices.getMDIManager().getActiveWindow();
80
81 9807 caballero
                if (e.getID() == KeyEvent.KEY_PRESSED || !(view instanceof Layout) || !(e.getSource() instanceof LayoutControl))
82 7304 caballero
                        return false;
83
                Layout layout=(Layout)view;
84 9392 caballero
                if (!layout.getLayoutContext().isEditable())
85 7304 caballero
                        return false;
86 9392 caballero
                IFFrame[] fframes = layout.getLayoutContext().getFFrameSelected();
87 7304 caballero
                int dif = 10;
88
                if (e.getModifiers() == KeyEvent.CTRL_MASK) {
89
                        switch (e.getKeyCode()) {
90
                        case KeyEvent.VK_C:
91 9807 caballero
                                copy(layout);
92 7304 caballero
                                break;
93
                        case KeyEvent.VK_X:
94 9807 caballero
                                cut(layout);
95 7304 caballero
                                break;
96
                        case KeyEvent.VK_V:
97 9807 caballero
                                paste(layout);
98 7304 caballero
                                break;
99
                        case KeyEvent.VK_Z:
100 9807 caballero
                                undo(layout);
101 7304 caballero
                                break;
102
                        case KeyEvent.VK_Y:
103 9807 caballero
                                redo(layout);
104 7304 caballero
                                break;
105
                        }
106
                } else {
107
                        switch (e.getKeyCode()) {
108
                        case KeyEvent.VK_LEFT:
109
                                difX = difX - dif;
110
                                break;
111
112
                        case KeyEvent.VK_RIGHT:
113
                                difX = difX + dif;
114
                                break;
115
                        case KeyEvent.VK_UP:
116
                                difY = difY - dif;
117
                                break;
118
                        case KeyEvent.VK_DOWN:
119
                                difY = difY + dif;
120
                                break;
121
                        case KeyEvent.VK_DELETE:
122
                        case KeyEvent.VK_BACK_SPACE:
123 9392 caballero
                                layout.getLayoutContext().delFFrameSelected();
124
                                layout.getLayoutControl().refresh();
125 7304 caballero
                                break;
126
                        }
127
                        if (e.getKeyCode()==KeyEvent.VK_LEFT || e.getKeyCode()==KeyEvent.VK_RIGHT || e.getKeyCode()==KeyEvent.VK_UP || e.getKeyCode()==KeyEvent.VK_DOWN) {
128
                                if (fframes.length==0)
129
                                        return false;
130 9392 caballero
                                layout.getLayoutContext().getEFS().startComplexCommand();
131 7304 caballero
                        for (int i = 0; i < fframes.length; i++) {
132
                                IFFrame fframeAux = fframes[i].cloneFFrame(layout);
133
                                Rectangle2D r = getRectMove(fframes[i]
134 9392 caballero
                                                .getBoundingBox(layout.getLayoutControl().getAT()), difX, difY);
135 7304 caballero
                                fframeAux.setBoundBox(FLayoutUtilities.toSheetRect(r,
136 9392 caballero
                                                layout.getLayoutControl().getAT()));
137
                                layout.getLayoutContext().getEFS().modifyFFrame(fframes[i], fframeAux);
138 7304 caballero
                        }
139 9392 caballero
                        layout.getLayoutContext().getEFS().endComplexCommand(
140 7304 caballero
                                        PluginServices.getText(this, "move_elements"));
141 9392 caballero
                        layout.getLayoutContext().updateFFrames();
142
                        layout.getLayoutControl().refresh();
143 7304 caballero
                        }
144
                        difX = 0;
145
                        difY = 0;
146
147
                }
148
                return true;
149
        }
150
        private Rectangle2D getRectMove(Rectangle2D r,int difX,int difY) {
151
                Rectangle2D rectMove=new Rectangle2D.Double();
152
                rectMove.setRect(r.getX()+difX,r.getY()+difY,r.getWidth(),r.getHeight());
153
                return rectMove;
154
        }
155 9807 caballero
        public static boolean hasSelection() {
156
                return selectFFrames.length>0;
157
        }
158 7304 caballero
159
}