Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e418780057a9b5e03a40f4b74d536c3e586943f3 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2006 Phil Muldoon <pkmuldoon@picobot.org>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Phil Muldoon <pmuldoon@redhat.com> - initial API and implementation
 *    Kyu Lee <klee@redhat.com>          - new execute method
 *******************************************************************************/
package org.eclipse.linuxtools.internal.changelog.core.actions;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.linuxtools.changelog.core.IParserChangeLogContrib;
import org.eclipse.linuxtools.internal.changelog.core.ChangeLogWriter;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;


/**
 * @author pmuldoon (Phil Muldoon)
 */
public class InsertChangeLogKeyHandler extends ChangeLogAction implements
        IHandler, IWorkbenchWindowActionDelegate {

    private IEditorPart currentEditor;

    private String getEditorName() {
        if (currentEditor != null) {
            return returnQualifedEditor(currentEditor.getClass());
        } else {
            return "";
        }

    }

    private String getEntryFilePath() {
        if (currentEditor != null) {
            return getDocumentLocation(currentEditor, false);
        } else {
            return "";
        }
    }

    private String returnQualifedEditor(Class<?> className) {
        return className.toString().substring(
                className.getPackage().toString().length() - 1,
                className.toString().length());
    }


    private IEditorPart getChangelog() {

        IConfigurationElement formatterConfigElement = extensionManager
                .getFormatterConfigElement();
        if (formatterConfigElement.getAttribute("inFile").equalsIgnoreCase(
                "true")) {
            return currentEditor;
            // this formatter wants to use an external changelog file
        } else {
            IEditorPart changelog = null;

            IConfigurationElement nameElement = formatterConfigElement
                    .getChildren()[0];
            if (nameElement.getAttribute("name") == null) {
                reportErr("Got non-name child with inFile set to False", null);
                return null;
            } else {
                pref_ChangeLogName = nameElement.getAttribute("name");
                changelog = getChangelog(getDocumentLocation(currentEditor,
                        false));

                if (changelog == null) {
                    changelog = askChangeLogLocation(getDocumentLocation(
                            currentEditor, false));
                }

                return changelog;
            }
        }

    }

    private String parseFunctionName(IParserChangeLogContrib parser) {

        try {
            return parser.parseCurrentFunction(currentEditor);
        } catch (CoreException e) {
            reportErr("Couldn't parse function name with "
                    + parser.getClass().toString(), null);
            return "";
        }

    }

    @Override
    public Object execute(ExecutionEvent event) {

        currentEditor = HandlerUtil.getActiveEditor(event);

        // make sure an editor is selected.
        if (currentEditor == null) {
            return null;
        }

        ChangeLogWriter clw = new ChangeLogWriter();

        // load settings from extensions + user pref.
        loadPreferences();

        // get file path from target file
        clw.setEntryFilePath(getEntryFilePath());

        // err check. do nothing if no file is being open/edited
        if (clw.getEntryFilePath() == "") {
            return null;
        }

        String editorName = getEditorName();

        // get a parser for this file
        IParserChangeLogContrib parser = extensionManager
                .getParserContributor(editorName);

        // if no parser for this type of document, then don't guess function
        // name
        // and set it as "".
        if (parser == null) {
            clw.setGuessedFName("");
        } else {
            // guess function name
            clw.setGuessedFName(parseFunctionName(parser));
        }

        // get formatter
        clw.setFormatter(extensionManager.getFormatterContributor(clw
                .getEntryFilePath(), pref_Formatter));

        // select changelog
        clw.setChangelog(getChangelog());
        if (clw.getChangelog() == null)
            return null;

        // write to changelog
        clw.setDateLine(clw.getFormatter().formatDateLine(pref_AuthorName,
                pref_AuthorEmail));

        clw.setChangelogLocation(getDocumentLocation(clw.getChangelog(), true));

        clw.writeChangeLog();

        return null;
    }

    @Override
    public void addHandlerListener(IHandlerListener handlerListener) {

    }

    @Override
    public boolean isEnabled() {
        IEditorReference[] refs = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
        for (int i = 0; i < refs.length; ++i) {
            IEditorReference ref = refs[i];
            String id = ref.getId();
            System.out.println(id);
        }
        return true;
    }

    @Override
    public boolean isHandled() {
        return true;
    }

    @Override
    public void removeHandlerListener(IHandlerListener handlerListener) {

    }

    @Override
    public void dispose() {

    }

    @Override
    public void init(IWorkbenchWindow window) {

    }

    @Override
    public void run(IAction action) {

            execute(null);
    }

    @Override
    public void selectionChanged(IAction action, ISelection selection) {

    }
}

Back to the top