source: trunk/autoquest-ui-swt/src/main/java/de/ugoe/cs/autoquest/ui/swt/DataTabComposite.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 4.9 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.ui.swt;
16
17import org.eclipse.swt.SWT;
18import org.eclipse.swt.widgets.Composite;
19import org.eclipse.swt.widgets.Button;
20import org.eclipse.swt.widgets.FileDialog;
21import org.eclipse.swt.widgets.List;
22import org.eclipse.swt.widgets.MessageBox;
23import org.eclipse.swt.layout.GridLayout;
24import org.eclipse.swt.layout.GridData;
25
26import de.ugoe.cs.util.StringTools;
27import de.ugoe.cs.util.console.CommandExecuter;
28import de.ugoe.cs.util.console.GlobalDataContainer;
29
30import org.eclipse.swt.events.SelectionAdapter;
31import org.eclipse.swt.events.SelectionEvent;
32
33public class DataTabComposite extends Composite {
34
35    List dataList;
36
37    /**
38     * Create the composite.
39     *
40     * @param parent
41     * @param style
42     */
43    public DataTabComposite(Composite parent, int style) {
44        super(parent, style);
45        createContent();
46    }
47
48    private void createContent() {
49        setLayout(new GridLayout(3, false));
50
51        dataList = new List(this, SWT.BORDER | SWT.V_SCROLL);
52        dataList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
53
54        Button btnLoad = new Button(this, SWT.NONE);
55        btnLoad.addSelectionListener(new SelectionAdapter() {
56            @Override
57            public void widgetSelected(SelectionEvent e) {
58                GetObjectNameDialog getObjectNameDialog =
59                    new GetObjectNameDialog(getShell(), SWT.NONE);
60                getObjectNameDialog.open();
61                String objectName = getObjectNameDialog.getObjectName();
62                if ("".equals(objectName)) {
63                    return;
64                }
65                FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN);
66                String filename = fileDialog.open();
67                if (filename == null) {
68                    return;
69                }
70                String command = "loadObject " + filename + " " + objectName;
71                CommandExecuter.getInstance().exec(command);
72                updateDataList();
73            }
74        });
75        btnLoad.setText("Load");
76
77        Button btnSave = new Button(this, SWT.NONE);
78        btnSave.addSelectionListener(new SelectionAdapter() {
79            @Override
80            public void widgetSelected(SelectionEvent e) {
81                String[] selectedStrings = dataList.getSelection();
82                if (selectedStrings.length == 0) {
83                    SWTHelpers.noSelectionError(getShell());
84                    return;
85                }
86                if (selectedStrings.length > 1) {
87                    MessageBox messageBox = new MessageBox(getShell(), SWT.ERROR);
88                    messageBox.setText("Error");
89                    messageBox.setMessage("Only one object storable at a time." +
90                        StringTools.ENDLINE + "Please select only one object.");
91                    return;
92                }
93                FileDialog fileDialog = new FileDialog(getShell(), SWT.SAVE);
94                String filename = fileDialog.open();
95                if (filename == null) {
96                    return;
97                }
98                String command = "saveObject " + filename + " " + selectedStrings[0];
99                CommandExecuter.getInstance().exec(command);
100            }
101        });
102        btnSave.setText("Save");
103
104        Button btnDelete_2 = new Button(this, SWT.NONE);
105        btnDelete_2.addSelectionListener(new SelectionAdapter() {
106            @Override
107            public void widgetSelected(SelectionEvent e) {
108                if (SWTHelpers.deleteSelectedFromStorage(dataList)) {
109                    updateDataList();
110                }
111                else {
112                    SWTHelpers.noSelectionError(getShell());
113                }
114            }
115        });
116        btnDelete_2.setText("Delete");
117    }
118
119    @Override
120    protected void checkSubclass() {
121        // Disable the check that prevents subclassing of SWT components
122    }
123
124    public void updateDataList() {
125        dataList.removeAll();
126        for (String key : GlobalDataContainer.getInstance().getAllKeys()) {
127            dataList.add(key + " (" + GlobalDataContainer.getInstance().getData(key).getClass().toString() + ")");
128        }
129    }
130
131}
Note: See TracBrowser for help on using the repository browser.