2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
43
package org.netbeans.modules.i18n;
45
import java.awt.Dialog;
46
import java.awt.EventQueue;
47
import java.awt.event.ActionEvent;
48
import java.awt.event.ActionListener;
49
import java.io.IOException;
50
import java.lang.ref.WeakReference;
51
import javax.swing.JEditorPane;
52
import javax.swing.JPanel;
53
import javax.swing.SwingUtilities;
54
import javax.swing.text.Caret;
56
import org.openide.cookies.EditorCookie;
57
import org.openide.DialogDescriptor;
58
import org.openide.loaders.DataObject;
59
import org.openide.NotifyDescriptor;
60
import org.openide.DialogDisplayer;
61
import org.netbeans.api.project.Project;
62
import org.openide.util.RequestProcessor;
63
import org.openide.ErrorManager;
67
* Manages performing of i18n action -> i18n-zation of one source.
69
* @author Peter Zavadsky
71
public class I18nManager {
73
/** Singleton instance of I18nManager. */
74
private static I18nManager manager;
76
/** Support for this internatioanlize session. */
77
private I18nSupport support;
79
/** Weak reference to i18n panel. */
80
private WeakReference<I18nPanel> i18nPanelWRef = new WeakReference<I18nPanel>(null);
82
/** Weak reference to top component in which internationalizing will be provided. */
83
private WeakReference<Dialog> dialogWRef = new WeakReference<Dialog>(null);
85
/** Weak reference to caret in editor pane. */
86
private WeakReference<Caret> caretWRef;
88
/** Found hard coded string. */
89
private HardCodedString hcString;
92
/** Private constructor. To ge instance use <code>getI18nMananger</code> method instead. */
93
private I18nManager() {
97
/** Gets the only instance of I18nSupport. */
98
public static synchronized I18nManager getDefault() {
99
if (manager == null) {
100
manager = new I18nManager();
105
/** Get i18n support. */
106
private void initSupport(DataObject sourceDataObject) throws IOException {
107
I18nSupport.Factory factory = FactoryRegistry.getFactory(sourceDataObject.getClass());
109
support = factory.create(sourceDataObject);
112
/** The 'heart' method called by <code>I18nAction</code>. */
113
public void internationalize(final DataObject sourceDataObject) {
115
// If there is i18n action working -> cancel it.
118
// Initilialize support.
120
initSupport(sourceDataObject);
121
} catch (IOException ioe) {
122
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
126
// initialize the component
127
final EditorCookie ec = sourceDataObject.getCookie(EditorCookie.class);
132
// Add i18n panel to top component.
133
getDialog(sourceDataObject);
134
final I18nPanel i18nPanel = i18nPanelWRef.get();
135
i18nPanel.showBundleMessage("TXT_SearchingForStrings"); //NOI18N
136
i18nPanel.getCancelButton().requestFocusInWindow();
138
final class SearchResultDisplayer implements Runnable {
139
private final boolean success;
140
SearchResultDisplayer(boolean success) {
141
this.success = success;
148
i18nPanel.getReplaceButton().requestFocusInWindow();
150
i18nPanel.showBundleMessage("TXT_NoHardcodedString");//NOI18N
151
i18nPanel.getCancelButton().requestFocusInWindow();
157
// do the search on background
158
RequestProcessor.getDefault().post(new Runnable() {
160
boolean found = find();
161
EventQueue.invokeLater(new SearchResultDisplayer(found));
169
/** Initializes caret. */
170
private void initCaret(EditorCookie ec) {
171
JEditorPane[] panes = ec.getOpenedPanes();
173
NotifyDescriptor.Message message = new NotifyDescriptor.Message(
174
I18nUtil.getBundle().getString("MSG_CouldNotOpen"), //NOI18N
175
NotifyDescriptor.ERROR_MESSAGE);
176
DialogDisplayer.getDefault().notify(message);
180
// Keep only weak ref to caret, the strong one maintains editor pane itself.
181
caretWRef = new WeakReference<Caret>(panes[0].getCaret());
184
/** Highlights found hasrdcoded string. */
185
private void highlightHCString() {
186
HardCodedString hStr = hcString;
192
// Highlight found hard coded string.
193
Caret caret = caretWRef.get();
196
caret.setDot(hStr.getStartPosition().getOffset());
197
caret.moveDot(hStr.getEndPosition().getOffset());
201
/** Finds hard coded string. */
202
private boolean find() {
203
// Actual find on finder.
204
hcString = support.getFinder().findNextHardCodedString();
206
if (hcString != null) {
210
// not found in entire source document
214
/** Fills values presented in internationalize dialog. */
215
private void fillDialogValues() {
216
// It has to work this way, at this time the strong reference in top component have to exist.
217
I18nPanel i18nPanel = i18nPanelWRef.get();
219
i18nPanel.setI18nString(support.getDefaultI18nString(hcString));
224
/** Replaces current found hard coded string and continue the search for next one. */
225
private void replace() {
226
I18nString i18nString = null;
229
// To call weak without check have to be save here cause strong reference in the top component have to exist.
230
i18nString = i18nPanelWRef.get().getI18nString();
231
} catch (IllegalStateException e) {
232
NotifyDescriptor.Message nd = new NotifyDescriptor.Message(
233
I18nUtil.getBundle().getString("EXC_BadKey"), //NOI18N
234
NotifyDescriptor.ERROR_MESSAGE);
235
DialogDisplayer.getDefault().notify(nd);
239
// Try to add key to bundle.
240
support.getResourceHolder().addProperty(i18nString.getKey(), i18nString.getValue(), i18nString.getComment());
242
// Provide additional changes if they are available.
243
if (support.hasAdditionalCustomizer()) {
244
support.performAdditionalChanges();
247
// Replace hardcoded string.
248
support.getReplacer().replace(hcString, i18nString);
250
SwingUtilities.invokeLater(new Runnable() {
257
/** Skips foudn hard coded string and conitnue to search for next one. */
258
private void skip() {
263
i18nPanelWRef.get().showBundleMessage("TXT_NoMoreStrings"); //NOI18N
264
i18nPanelWRef.get().getCancelButton().requestFocusInWindow();
268
/** Shows info about found hard coded string. */
269
private void showInfo() {
270
JPanel infoPanel = support.getInfo(hcString);
272
DialogDescriptor dd = new DialogDescriptor(
274
I18nUtil.getBundle().getString("CTL_InfoPanelTitle")); //NOI18N
277
dd.setOptionType(DialogDescriptor.DEFAULT_OPTION);
278
dd.setOptions(new Object[] {DialogDescriptor.OK_OPTION});
279
dd.setAdditionalOptions(new Object[0]);
282
Dialog infoDialog = DialogDisplayer.getDefault().createDialog(dd);
283
infoDialog.setVisible(true);
286
/** Cancels current internationalizing session and re-layout top component to original layout. */
287
public void cancel() {
294
/** Gets dialog. In our case it is a top component.
295
* @param name name of top component */
296
private void getDialog(DataObject sourceDataObject) {
297
Project project = Util.getProjectFor(sourceDataObject);
299
Dialog dialog = dialogWRef.get();
300
I18nPanel i18nPanel = i18nPanelWRef.get();
302
// Dialog was not created yet or garbaged already.
303
if (i18nPanel == null) {
305
// Create i18n panel.
306
i18nPanel = new I18nPanel(support.getPropertyPanel(),
308
sourceDataObject.getPrimaryFile());
311
final I18nPanel panel = i18nPanel;
313
// Set button listeners.
314
ActionListener listener = new ActionListener() {
315
public void actionPerformed(ActionEvent evt) {
316
final Object source = evt.getSource();
317
if (source == panel.getReplaceButton()) {
319
} else if (source == panel.getSkipButton()) {
321
} else if (source == panel.getInfoButton()) {
323
} else if (source == panel.getCancelButton()) {
329
i18nPanel.getReplaceButton().addActionListener(listener);
330
i18nPanel.getSkipButton().addActionListener(listener);
331
i18nPanel.getInfoButton().addActionListener(listener);
332
i18nPanel.getCancelButton().addActionListener(listener);
334
// Reset weak reference.
335
i18nPanelWRef = new WeakReference<I18nPanel>(i18nPanel);
338
// i18nPanel.setProject(project);
339
i18nPanel.setFile(sourceDataObject.getPrimaryFile());
342
// Set default i18n string.
343
i18nPanel.setI18nString(support.getDefaultI18nString());
344
i18nPanel.setDefaultResource(sourceDataObject);
347
if (dialog == null) {
348
String title = Util.getString("CTL_I18nDialogTitle"); // NOI18N
349
DialogDescriptor dd = new DialogDescriptor(
355
DialogDescriptor.DEFAULT_ALIGN,
358
dialog = DialogDisplayer.getDefault().createDialog(dd);
359
dialog.setLocation(80, 80);
360
dialogWRef = new WeakReference<Dialog>(dialog);
363
dialog.setVisible(true);
366
/** Shows dialog. In our case opens top component if it is necessary and
367
* sets caret visible in editor part. */
368
private void showDialog() {
369
// Open dialog if available
370
Dialog dialog = dialogWRef.get();
371
if (dialog != null) {
372
dialog.setVisible(true);
375
// Set caret visible.
376
Caret caret = caretWRef.get();
378
if (!caret.isVisible()) {
379
caret.setVisible(true);
384
/** Closes dialog. In our case removes <code>I18nPanel</code> from top component
385
* and 'reconstruct it' to it's original layout. */
386
private void closeDialog() {
387
Dialog dialog = dialogWRef.get();
388
if (dialog != null) {
389
dialog.setVisible(false);