ludc
2025-01-16 68fd566d21b3efc3a670a5295289b1801f5a4155
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package qq.builder;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugModelPresentation;
import org.eclipse.debug.ui.ILaunchShortcut2;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.internal.core.CompilationUnit;
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
import org.eclipse.jdt.internal.debug.ui.launcher.LauncherMessages;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
 
public abstract class JavaClientLaunchShortcut implements ILaunchShortcut2 {
    private String javaProject = "";
 
    public String getJavaProject() {
        return javaProject;
    }
 
    public void setJavaProject(String javaProject) {
        this.javaProject = javaProject;
    }
 
    protected abstract ILaunchConfigurationType getConfigurationType();
 
    protected abstract ILaunchConfiguration createConfiguration(IType paramIType);
 
    protected abstract IType[] findTypes(Object[] paramArrayOfObject,
            IRunnableContext paramIRunnableContext)
            throws InterruptedException, CoreException;
 
    protected abstract String getTypeSelectionTitle();
 
    protected abstract String getEditorEmptyMessage();
 
    protected abstract String getSelectionEmptyMessage();
 
    private void searchAndLaunch(Object[] scope, String mode,
            String selectTitle, String emptyMessage) {
        // IType[] types = (IType[]) null;
        // try {
        // types = findTypes(scope, PlatformUI.getWorkbench()
        // .getProgressService());
        // } catch (InterruptedException localInterruptedException) {
        // return;
        // } catch (CoreException e) {
        // MessageDialog.openError(getShell(),
        // LauncherMessages.JavaLaunchShortcut_0, e.getMessage());
        // return;
        // }
        // BinaryType binaryType = new BinaryType(scope[0],);
        IType type = null;
        // if (types.length == 0) {
        // MessageDialog.openError(getShell(),
        // LauncherMessages.JavaLaunchShortcut_1, emptyMessage);
        // } else if (types.length > 1) {
        // type = chooseType(types, selectTitle);
        // } else {
        // type = types[0];
        // }
        // if (type != null)
        if (scope[0] instanceof CompilationUnit) {
            CompilationUnit c = (CompilationUnit) scope[0];
            setJavaProject(((IJavaProject) c.getAncestor(2)).getElementName());
        } else {
            setJavaProject(((IJavaProject) scope[0]).getElementName());
        }
 
        launch(type, mode);
    }
 
    protected IType chooseType(IType[] types, String title) {
        for (IType type : types) {
            if (type.isBinary()
                    && type.getElementName().equals("LogonApplication")) {
                return type;
            }
        }
        // DebugTypeSelectionDialog mmsd = new
        // DebugTypeSelectionDialog(JDIDebugUIPlugin.getShell(), types, title);
        // if (mmsd.open() == 0) {
        // return ((IType)mmsd.getResult()[0]);
        // }
        return null;
    }
 
    protected void launch(IType type, String mode) {
        ILaunchConfiguration config = findLaunchConfiguration(type,
                getConfigurationType());
        if (config == null) {
            config = createConfiguration(type);
        }
        if (config != null)
            DebugUITools.launch(config, mode);
    }
 
    protected ILaunchConfiguration findLaunchConfiguration(IType type,
            ILaunchConfigurationType configType) {
        List candidateConfigs = Collections.EMPTY_LIST;
        try {
            ILaunchConfiguration[] configs = DebugPlugin.getDefault()
                    .getLaunchManager().getLaunchConfigurations(configType);
            candidateConfigs = new ArrayList(configs.length);
            for (int i = 0; i < configs.length; ++i) {
                ILaunchConfiguration config = configs[i];
                if ((!(config.getAttribute(
                        IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
                        "")
                        .equals("com.vci.rmip.logon.client.LogonApplication")))
                        || (!(config
                                .getAttribute(
                                        IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
                                        "").equals(getJavaProject()))))
                    continue;
                candidateConfigs.add(config);
            }
        } catch (CoreException e) {
            JDIDebugUIPlugin.log(e);
        }
        int candidateCount = candidateConfigs.size();
        if (candidateCount == 1)
            return ((ILaunchConfiguration) candidateConfigs.get(0));
        if (candidateCount > 1) {
            return chooseConfiguration(candidateConfigs);
        }
        return null;
    }
 
    protected ILaunchConfiguration chooseConfiguration(List configList) {
        IDebugModelPresentation labelProvider = DebugUITools
                .newDebugModelPresentation();
        ElementListSelectionDialog dialog = new ElementListSelectionDialog(
                getShell(), labelProvider);
        dialog.setElements(configList.toArray());
        dialog.setTitle(getTypeSelectionTitle());
        dialog.setMessage(LauncherMessages.JavaLaunchShortcut_2);
        dialog.setMultipleSelection(false);
        int result = dialog.open();
        labelProvider.dispose();
        if (result == 0) {
            return ((ILaunchConfiguration) dialog.getFirstResult());
        }
        return null;
    }
 
    protected Shell getShell() {
        return JDIDebugUIPlugin.getActiveWorkbenchShell();
    }
 
    public void launch(IEditorPart editor, String mode) {
        IEditorInput input = editor.getEditorInput();
        IJavaElement je = (IJavaElement) input.getAdapter(IJavaElement.class);
        if (je != null)
            searchAndLaunch(new Object[] { je }, mode, getTypeSelectionTitle(),
                    getEditorEmptyMessage());
    }
 
    public void launch(ISelection selection, String mode) {
        if (selection instanceof IStructuredSelection)
            searchAndLaunch(((IStructuredSelection) selection).toArray(), mode,
                    getTypeSelectionTitle(), getSelectionEmptyMessage());
    }
 
    public IResource getLaunchableResource(IEditorPart editorpart) {
        return getLaunchableResource(editorpart.getEditorInput());
    }
 
    public IResource getLaunchableResource(ISelection selection) {
        if (selection instanceof IStructuredSelection) {
            IStructuredSelection ss = (IStructuredSelection) selection;
            if (ss.size() == 1) {
                Object element = ss.getFirstElement();
                if (element instanceof IAdaptable) {
                    return getLaunchableResource((IAdaptable) element);
                }
            }
        }
        return null;
    }
 
    private IResource getLaunchableResource(IAdaptable adaptable) {
        IJavaElement je = (IJavaElement) adaptable
                .getAdapter(IJavaElement.class);
        if (je != null) {
            return je.getResource();
        }
        return null;
    }
 
    public ILaunchConfiguration[] getLaunchConfigurations(IEditorPart editorpart) {
        return null;
    }
 
    public ILaunchConfiguration[] getLaunchConfigurations(ISelection selection) {
        return null;
    }
}