Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / LayoutKeyEvent.java @ 1600

History | View | Annotate | Download (8.12 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.app.project.documents.layout;
23

    
24
import java.awt.KeyEventDispatcher;
25
import java.awt.event.KeyEvent;
26
import java.awt.geom.Rectangle2D;
27

    
28
import org.gvsig.andami.PluginServices;
29
import org.gvsig.andami.messages.NotificationManager;
30
import org.gvsig.andami.ui.mdiManager.IWindow;
31
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
32
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
33
import org.gvsig.app.project.documents.view.toc.DnDJTree;
34
import org.gvsig.tools.undo.RedoException;
35
import org.gvsig.tools.undo.UndoException;
36

    
37
public class LayoutKeyEvent implements KeyEventDispatcher {
38

    
39
    private static IFFrame[] selectFFrames = new IFFrame[0];
40
    private int difX;
41
    private int difY;
42

    
43
    public static boolean copy(LayoutPanel layout)
44
        throws CloneNotSupportedException {
45
        IFFrame[] fframes = layout.getLayoutContext().getSelectedFFrames();
46
        if (fframes.length == 0)
47
            return false;
48
        selectFFrames = new IFFrame[fframes.length];
49
        for (int i = 0; i < fframes.length; i++) {
50
            selectFFrames[i] = (IFFrame) fframes[i].clone();
51
        }
52
        return true;
53
    }
54

    
55
    public static boolean cut(LayoutPanel layout)
56
        throws CloneNotSupportedException {
57
        IFFrame[] fframes = layout.getLayoutContext().getSelectedFFrames();
58
        if (fframes.length == 0)
59
            return false;
60
        selectFFrames = new IFFrame[fframes.length];
61
        for (int i = 0; i < fframes.length; i++) {
62
            selectFFrames[i] = (IFFrame) fframes[i].clone();
63
        }
64
        layout.getLayoutContext().delFFrameSelected();
65
        layout.getLayoutContext().notifAllObservers();        
66
        return true;
67
    }
68

    
69
    public static boolean paste(LayoutPanel layout)
70
        throws CloneNotSupportedException {
71
        IFFrame copyFFrame = null;
72
        layout.getLayoutContext().getFrameCommandsRecord()
73
            .startComplex(PluginServices.getText(layout, "paste_elements"));
74
        for (int i = 0; i < selectFFrames.length; i++) {
75
            copyFFrame = (IFFrame) selectFFrames[i].clone();
76
            if (i == 0)
77
                layout.getLayoutContext().addFFrame(copyFFrame, true, true);
78
            else
79
                layout.getLayoutContext().addFFrame(copyFFrame, false, true);
80
            copyFFrame.setDocument(layout.getDocument());
81
        }
82
        layout.getLayoutContext().getFrameCommandsRecord().endComplex();
83
        layout.getLayoutContext().notifAllObservers();      
84
        return true;
85
    }
86

    
87
    public static boolean undo(LayoutPanel layout) {
88
        LayoutContext lcontext = layout.getLayoutContext();
89
        try {
90
            lcontext.getFrameCommandsRecord().undo();
91
        } catch (UndoException e) {
92
            e.printStackTrace();
93
        }
94
        layout.getLayoutContext().updateFFrames();
95
        layout.getLayoutContext().notifAllObservers();      
96
        PluginServices.getMainFrame().enableControls();
97
        return true;
98
    }
99

    
100
    public static boolean redo(LayoutPanel layout) {
101
        LayoutContext lcontext = layout.getLayoutContext();
102
        try {
103
            lcontext.getFrameCommandsRecord().redo();
104
        } catch (RedoException e) {
105
            e.printStackTrace();
106
        }
107
        lcontext.updateFFrames();
108
        layout.getLayoutContext().notifAllObservers();      
109
        PluginServices.getMainFrame().enableControls();
110
        return true;
111
    }
112

    
113
    public boolean dispatchKeyEvent(KeyEvent e) {
114
        IWindow view = PluginServices.getMDIManager().getActiveWindow();
115

    
116
        try {
117
            if (e.getID() == KeyEvent.KEY_PRESSED
118
                || !(view instanceof LayoutPanel)
119
                || !( (e.getSource() instanceof LayoutControl)
120
                                || (e.getSource() instanceof DnDJTree)))
121
                return false;
122
            LayoutPanel layout = (LayoutPanel) view;
123
            if (!layout.getLayoutContext().isEditable())
124
                return false;
125
            IFFrame[] fframes = layout.getLayoutContext().getSelectedFFrames();
126
            int dif = 10;
127
            if (e.getModifiers() == KeyEvent.CTRL_MASK) {
128
                switch (e.getKeyCode()) {
129
                case KeyEvent.VK_C:
130
                    copy(layout);
131
                    break;
132
                case KeyEvent.VK_X:
133
                    cut(layout);
134
                    break;
135
                case KeyEvent.VK_V:
136
                    paste(layout);
137
                    break;
138
                case KeyEvent.VK_Z:
139
                    undo(layout);
140
                    break;
141
                case KeyEvent.VK_Y:
142
                    redo(layout);
143
                    break;
144
                }
145
            } else {
146
                switch (e.getKeyCode()) {
147
                case KeyEvent.VK_LEFT:
148
                    difX = difX - dif;
149
                    break;
150

    
151
                case KeyEvent.VK_RIGHT:
152
                    difX = difX + dif;
153
                    break;
154
                case KeyEvent.VK_UP:
155
                    difY = difY - dif;
156
                    break;
157
                case KeyEvent.VK_DOWN:
158
                    difY = difY + dif;
159
                    break;
160
                case KeyEvent.VK_DELETE:
161
                case KeyEvent.VK_BACK_SPACE:
162
                    layout.getLayoutContext().delFFrameSelected();
163
                    layout.getLayoutControl().refresh();
164
                    break;
165
                }
166
                if (e.getKeyCode() == KeyEvent.VK_LEFT
167
                    || e.getKeyCode() == KeyEvent.VK_RIGHT
168
                    || e.getKeyCode() == KeyEvent.VK_UP
169
                    || e.getKeyCode() == KeyEvent.VK_DOWN) {
170
                    if (fframes.length == 0)
171
                        return false;
172
                    layout
173
                        .getLayoutContext()
174
                        .getFrameCommandsRecord()
175
                        .startComplex(
176
                            PluginServices.getText(this, "move_elements"));
177
                    for (int i = 0; i < fframes.length; i++) {
178
                        IFFrame fframeAux = (IFFrame) fframes[i].clone();
179
                        Rectangle2D r =
180
                            getRectMove(fframes[i].getBoundingBox(layout
181
                                .getLayoutControl().getAT()), difX, difY);
182
                        fframeAux.setBoundBox(FLayoutUtilities.toSheetRect(r,
183
                            layout.getLayoutControl().getAT()));
184
                        layout.getLayoutContext().getFrameCommandsRecord()
185
                            .update(fframes[i], fframeAux);
186
                    }
187
                    layout.getLayoutContext().getFrameCommandsRecord()
188
                        .endComplex();
189
                    layout.getLayoutContext().updateFFrames();
190
                    layout.getLayoutControl().refresh();
191
                }
192
                difX = 0;
193
                difY = 0;
194

    
195
            }
196
            return true;
197
        } catch (CloneNotSupportedException e1) {
198
            NotificationManager.addError(e1);
199
            return false;
200
        }
201
    }
202

    
203
    private Rectangle2D getRectMove(Rectangle2D r, int difX, int difY) {
204
        Rectangle2D rectMove = new Rectangle2D.Double();
205
        rectMove.setRect(r.getX() + difX, r.getY() + difY, r.getWidth(),
206
            r.getHeight());
207
        return rectMove;
208
    }
209

    
210
    public static boolean hasSelection() {
211
        return selectFFrames.length > 0;
212
    }
213

    
214
}