My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright 2011 stanislawbartkowski@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.db2.sb.scriptlauncher;

import java.net.URI;

import org.eclipse.core.internal.resources.File;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugModelPresentation;
import org.eclipse.debug.ui.ILaunchGroup;
import org.eclipse.debug.ui.ILaunchShortcut;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;

/**
* Launch shortcut
*
* @author sbartkowski
*
*/
public class DB2ScriptShortCut implements ILaunchShortcut {

private final static String NEW_CONFIGURATION = "New_configuration";

private Shell getShell() {
return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
}

/**
* Show a selection dialog that allows the user to choose one of the
* specified launch configurations. Return the chosen config, or
* <code>null</code> if the user canceled the dialog.
*/
protected ILaunchConfiguration chooseConfiguration(
ILaunchConfiguration[] configurations) {
IDebugModelPresentation labelProvider = DebugUITools
.newDebugModelPresentation();
ElementListSelectionDialog dialog = new ElementListSelectionDialog(
getShell(), labelProvider);
dialog.setElements(configurations);
dialog.setTitle(Messages.DB2ScriptTabView_List); //$NON-NLS-1$
dialog.setMessage(Messages.DB2ScriptTabView_Choose); //$NON-NLS-1$
dialog.setMultipleSelection(false);
int result = dialog.open();
labelProvider.dispose();
if (result == Window.OK) {
return (ILaunchConfiguration) dialog.getFirstResult();
}
return null;
}

/**
* Execute script
*
* @param script
* Script file name (if not null)
* @param content
* Script content (if not null)
*/
private void runDB2(String script, String content) {
ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType type = manager
.getLaunchConfigurationType("com.db2.sb.scriptlauncher.DB2ScriptLauncher");
ILaunchConfiguration[] configurations = null;
ILaunchConfigurationWorkingCopy workingCopy = null;
ILaunchConfiguration configuration = null;
try {
configurations = manager.getLaunchConfigurations(type);

if (configurations.length == 0) {
// Open launch configuration dialog for enter first configuration
String name = manager.generateLaunchConfigurationName(NEW_CONFIGURATION);
ILaunchConfigurationWorkingCopy wc = type.newInstance(null,
name);
configuration = wc.doSave();
LaunchConfigurationManager lcm = DebugUIPlugin.getDefault().getLaunchConfigurationManager();

ILaunchGroup group = lcm.getLaunchGroup(type, ILaunchManager.RUN_MODE);
DebugUITools.openLaunchConfigurationPropertiesDialog(getShell(), configuration, group.getIdentifier());
} else {
if (configurations.length == 1) {
configuration = configurations[0];
} else {
// Choose configuration (server) if more than one
configuration = chooseConfiguration(configurations);
if (configuration == null) {
return;
}
}
}

// get parameters, configuration
workingCopy = configuration.copy(configuration.getName());
workingCopy.setAttribute(DB2ScriptTabView.DB_SCRIPT_TO_RUN, script);
workingCopy.setAttribute(DB2ScriptTabView.DB_CONTENT_TO_RUN,
content);
configuration = workingCopy.doSave();

} catch (CoreException e) {
e.printStackTrace();
DB2LogUtil.launchererrorLog(e);
return;
}

// execute
DebugUITools.launch(configuration, ILaunchManager.RUN_MODE);

}

@SuppressWarnings("restriction")
@Override
public void launch(ISelection arg0, String arg1) {
ITreeSelection iSel = (ITreeSelection) arg0;
TreePath[] t = iSel.getPaths();
TreePath fPath = t[0];
Object o = fPath.getLastSegment();
File f = (File) o;
URI locationURI = f.getLocationURI();
URI u = locationURI;
String fileName = u.getPath();
runDB2(fileName, null);

}

@Override
public void launch(IEditorPart arg0, String arg1) {
ITextEditor i = (ITextEditor) arg0;
IDocumentProvider dProvider = i.getDocumentProvider();
IDocument iDok = dProvider.getDocument(i.getEditorInput());
String content = iDok.get();
runDB2(null, content);

}
}

Change log

r492 by stanislawbartkowski on Nov 28, 2011   Diff
Log reporting added and small changes
Go to: 
Project members, sign in to write a code review

Older revisions

r460 by stanislawbartkowski on Oct 7, 2011   Diff
First checkin
All revisions of this file

File info

Size: 5627 bytes, 164 lines

File properties

svn:mime-type
text/plain
Powered by Google Project Hosting