• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.ide.eclipse.adt.internal.build;
17 
18 import com.android.ide.eclipse.adt.internal.editors.IconFactory;
19 
20 import org.eclipse.jface.dialogs.IDialogConstants;
21 import org.eclipse.jface.dialogs.IMessageProvider;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.dialogs.TitleAreaDialog;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.SelectionListener;
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Link;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.ui.IWorkbench;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.browser.IWebBrowser;
38 
39 import java.net.URL;
40 
41 /**
42  * Dialog shown by the {@link ConvertSwitchQuickFixProcessor}. This is a custom
43  * dialog rather than a plain {@link MessageDialog} such that we can show a link
44  * and point to a web page for more info.
45  */
46 class ConvertSwitchDialog extends TitleAreaDialog implements SelectionListener {
47     /** URL containing more info */
48     private static final String URL = "http://tools.android.com/tips/non-constant-fields"; //$NON-NLS-1$
49 
50     private final String mField;
51 
52     private Link mLink;
53 
54     /**
55      * Create the dialog.
56      * @param parentShell the parent shell
57      * @param field the field name we're warning about
58      */
ConvertSwitchDialog(Shell parentShell, String field)59     public ConvertSwitchDialog(Shell parentShell, String field) {
60         super(parentShell);
61         mField = field;
62         Image image = IconFactory.getInstance().getIcon("android-64"); //$NON-NLS-1$
63         setTitleImage(image);
64     }
65 
66     @Override
createDialogArea(Composite parent)67     protected Control createDialogArea(Composite parent) {
68         String text = String.format(
69             "As of ADT 14, the resource fields (such as %1$s) are no longer constants " +
70             "when defined in library projects. This is necessary to make library " +
71             "projects reusable without recompiling them.\n" +
72             "\n" +
73             "One consequence of this is that you can no longer use the fields directly " +
74             "in switch statements. You must use an if-else chain instead.\n" +
75             "\n" +
76             "Eclipse can automatically convert from a switch statement to an if-else " +
77             "statement. Just place the caret on the switch keyword and invoke " +
78             "Quick Fix (Ctrl-1 on Windows and Linux, Cmd-1 on Mac), then select " +
79             "\"Convert 'switch' to 'if-else'\".\n" +
80             "\n" +
81             "For more information, see <a href=\"" + URL + "\">" + URL + "</a>",
82             mField);
83 
84         Composite area = (Composite) super.createDialogArea(parent);
85         Composite container = new Composite(area, SWT.NONE);
86         container.setLayout(new GridLayout(1, false));
87         container.setLayoutData(new GridData(GridData.FILL_BOTH));
88 
89         mLink = new Link(container, SWT.NONE);
90         mLink.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1));
91         mLink.setText(text);
92         mLink.addSelectionListener(this);
93 
94         setMessage("Non-Constant Expressions: Migration Necessary", IMessageProvider.INFORMATION);
95 
96         return area;
97     }
98 
99     @Override
createButtonsForButtonBar(Composite parent)100     protected void createButtonsForButtonBar(Composite parent) {
101         createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
102         createButton(parent, IDialogConstants.HELP_ID, IDialogConstants.HELP_LABEL, false);
103     }
104 
105     @Override
getInitialSize()106     protected Point getInitialSize() {
107         return new Point(500, 400);
108     }
109 
showWebPage()110     private void showWebPage() {
111         try {
112             IWorkbench workbench = PlatformUI.getWorkbench();
113             IWebBrowser browser = workbench.getBrowserSupport().getExternalBrowser();
114             browser.openURL(new URL(URL));
115         } catch (Exception e) {
116             String message = String.format("Could not open browser. Vist\n%1$s\ninstead.",
117                     URL);
118             MessageDialog.openError(getShell(), "Browser Error", message);
119         }
120 
121     }
122 
123     @Override
buttonPressed(int buttonId)124     protected void buttonPressed(int buttonId) {
125         if (buttonId == IDialogConstants.HELP_ID) {
126             showWebPage();
127         } else {
128             super.buttonPressed(buttonId);
129         }
130     }
131 
132     // ---- Implements SelectionListener ----
133 
134     @Override
widgetSelected(SelectionEvent e)135     public void widgetSelected(SelectionEvent e) {
136         if (e.getSource() == mLink) {
137             showWebPage();
138         }
139     }
140 
141     @Override
widgetDefaultSelected(SelectionEvent e)142     public void widgetDefaultSelected(SelectionEvent e) {
143     }
144 }
145