View | Details | Raw Unified | Return to bug 75333 | Differences between
and this patch

Collapse All | Expand All

(-)plugin.xml (-1 / +12 lines)
Lines 163-169 Link Here
163
	<fileTypes extension="jardesc" type="text"/>
163
	<fileTypes extension="jardesc" type="text"/>
164
	<fileTypes extension="zip" type="binary"/>
164
	<fileTypes extension="zip" type="binary"/>
165
</extension>
165
</extension>
166
      
166
167
<!-- =================================================================================== -->
168
<!-- Extension: Java Code Formatter                                                      -->
169
<!-- =================================================================================== -->
170
<extension
171
      id="JavaCodeFormatter"
172
      point="org.eclipse.core.runtime.applications">
173
      	<application>
174
      		<run class="org.eclipse.jdt.core.formatter.CodeFormatterApplication" />
175
		</application>
176
</extension>
177
167
<!-- =================================================================================== -->
178
<!-- =================================================================================== -->
168
<!-- Extension: Java Content Types                                                       -->
179
<!-- Extension: Java Content Types                                                       -->
169
<!-- =================================================================================== -->
180
<!-- =================================================================================== -->
(-)formatter/org/eclipse/jdt/core/formatter/CodeFormatter.java (-1 / +1 lines)
Lines 79-85 Link Here
79
	 *      level of zero or below has no effect.
79
	 *      level of zero or below has no effect.
80
	 * @param lineSeparator the line separator to use in formatted source,
80
	 * @param lineSeparator the line separator to use in formatted source,
81
	 *     if set to <code>null</code>, then the platform default one will be used.
81
	 *     if set to <code>null</code>, then the platform default one will be used.
82
	 * @return the text edit
82
	 * @return the text edit or <code>null</code> if the given string cannot be formatted.
83
	 * @throws IllegalArgumentException if offset is lower than 0, length is lower than 0 or
83
	 * @throws IllegalArgumentException if offset is lower than 0, length is lower than 0 or
84
	 * length is greater than source length.
84
	 * length is greater than source length.
85
	 */
85
	 */
(-)formatter/org/eclipse/jdt/core/formatter/ProfileVersionNumbers.java (+24 lines)
Added Link Here
1
package org.eclipse.jdt.core.formatter;
2
3
/*
4
 *  These numbers need to be here so that the formatter application can check if
5
 *  the specified formatter config file is compatible. 
6
 * 
7
 */
8
public class ProfileVersionNumbers {
9
	
10
	private ProfileVersionNumbers() { /* shouldn't be instantiated */ }
11
	
12
	public static final int VERSION_1= 1; // < 20040113 (includes M6)
13
	public static final int VERSION_2= 2; // before renaming almost all
14
	public static final int VERSION_3= 3; // after renaming almost all
15
	public static final int VERSION_4= 4; 
16
	public static final int VERSION_5= 5; // after splitting of FORMATTER_INDENT_BLOCK_STATEMENTS
17
	public static final int VERSION_6= 6; // after splitting of new_line_in_control_statements
18
	public static final int VERSION_7= 7; // after moving comment formatter to JDT Core
19
	public static final int VERSION_8= 8; // fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=89739
20
	public static final int VERSION_9= 9; // after storing project profile names in preferences
21
	
22
	public static final int CURRENT_VERSION= VERSION_9;
23
	
24
}
(-)formatter/org/eclipse/jdt/core/formatter/FormatterAppMessages.properties (+43 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 Ben Konrath <ben@bagu.org>
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     Ben Konrath <ben@bagu.org> - initial implementation
10
 *******************************************************************************/
11
12
CommandLine.start=Starting format job ...
13
CommandLine.done=Done.
14
CommandLine.config.file=Configuration Name: {0}
15
CommandLine.formatting=Formatting: {0}
16
17
CommandLine.usage=Usage: {0} [ OPTIONS ] <files>
18
19
CommandLine.files=<files>
20
CommandLine.files.msg1=Java source files and/or directories to format.
21
CommandLine.files.msg2=Only files ending with {0} will be formatted in the given directory.
22
23
CommandLine.options=OPTIONS:
24
CommandLine.config={0} <file>
25
CommandLine.config.msg1=Use the formatting style from the specified config file.
26
CommandLine.config.msg2=This file must be an xml file that has been exported by Eclipse 3.0.
27
CommandLine.help=Display this message.
28
CommandLine.quiet=Only print error messages.
29
CommandLine.verbose=Be verbose about the formatting job.
30
31
CommandLineError.file={0} does not exsit. Please specify only valid Java Source files.
32
CommandLineError.config=There was problem reading the config file, {0}.
33
CommandLineError.file.dir=You must specify at least one file or dirctory to format.
34
CommandLineError.quiet.verbose=You cannot use the options {0} and {1} together.
35
36
Exception.io=Caught IOExecption:
37
Exception.bad.location=Caught BadLocationException:  
38
Exception.skip=Skipping File.
39
40
ConfigFile.reading.error=Error Reading config file.
41
42
Edit.problem=The Eclipse formatter had a problem {0}, Skipping.
43
(-)formatter/org/eclipse/jdt/core/formatter/CodeFormatterApplication.java (+422 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 Ben Konrath <ben@bagu.org>
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     Ben Konrath <ben@bagu.org> - initial implementation
10
 *     Red Hat Incorporated - add ProfileVersionNumbers.CURRENT_VERSION
11
 *******************************************************************************/
12
13
package org.eclipse.jdt.core.formatter;
14
15
import java.io.BufferedReader;
16
import java.io.BufferedWriter;
17
import java.io.File;
18
import java.io.FileInputStream;
19
import java.io.FileReader;
20
import java.io.FileWriter;
21
import java.io.IOException;
22
import java.io.Writer;
23
import java.text.MessageFormat;
24
import java.util.ArrayList;
25
import java.util.HashMap;
26
import java.util.Map;
27
import java.util.MissingResourceException;
28
import java.util.ResourceBundle;
29
30
import javax.xml.parsers.ParserConfigurationException;
31
import javax.xml.parsers.SAXParser;
32
import javax.xml.parsers.SAXParserFactory;
33
34
import org.eclipse.core.runtime.IPlatformRunnable;
35
import org.eclipse.core.runtime.Platform;
36
import org.eclipse.jdt.core.ToolFactory;
37
import org.eclipse.jdt.internal.core.util.Util;
38
import org.eclipse.jface.text.BadLocationException;
39
import org.eclipse.jface.text.Document;
40
import org.eclipse.jface.text.IDocument;
41
import org.eclipse.text.edits.TextEdit;
42
import org.xml.sax.Attributes;
43
import org.xml.sax.InputSource;
44
import org.xml.sax.SAXException;
45
import org.xml.sax.helpers.DefaultHandler;
46
47
/**
48
 * Class that handles the org.eclipse.jdt.core.JavaCodeFormatter the
49
 * application. The map file reading code is based on code in ProfileStore.java
50
 * (org.eclipse.jdf.ui).
51
 * 
52
 * There are a couple improvments that could be made:
53
 * 1. Add an import clean up option (requires stuff from org.eclipse.jdt.ui).
54
 * 2. Make a list of all the files first so that a file does not get formatted twice.
55
 * 3. Use a text based progress monitor for output.
56
 * 4. Allow using/updating older versions of the formatter config file. This requires
57
 *    a largish refactor because ProfilerVersioner is tied to the 
58
 *    org.eclipse.jdt.internal.ui.preferences code which is tied to several UI plugins.
59
 * 
60
 * @author Ben Konrath <ben@bagu.org>
61
 */
62
public class CodeFormatterApplication implements IPlatformRunnable {
63
64
    /**
65
     * A SAX event handler to parse the config xml.
66
     */
67
    private final static class ConfigHandler extends DefaultHandler {
68
69
        /**
70
         * Identifiers for the XML file.
71
         */
72
        private final String XML_NODE_ROOT = "profiles"; //$NON-NLS-1$
73
74
        private final String XML_NODE_PROFILE = "profile"; //$NON-NLS-1$
75
76
        private final String XML_NODE_SETTING = "setting"; //$NON-NLS-1$
77
78
        private final String XML_ATTRIBUTE_VERSION = "version"; //$NON-NLS-1$
79
80
        private final String XML_ATTRIBUTE_ID = "id"; //$NON-NLS-1$
81
82
        private final String XML_ATTRIBUTE_NAME = "name"; //$NON-NLS-1$
83
84
        private final String XML_ATTRIBUTE_VALUE = "value"; //$NON-NLS-1$
85
86
        private int fVersion;
87
88
        private String fName;
89
90
        private Map fSettings;
91
92
        public void startElement(String uri, String localName, String qName,
93
                Attributes attributes) throws SAXException {
94
95
            if (qName.equals(XML_NODE_SETTING)) {
96
97
                final String key = attributes.getValue(XML_ATTRIBUTE_ID);
98
                final String value = attributes.getValue(XML_ATTRIBUTE_VALUE);
99
                fSettings.put(key, value);
100
101
            } else if (qName.equals(XML_NODE_PROFILE)) {
102
103
                fName = attributes.getValue(XML_ATTRIBUTE_NAME);
104
                fSettings = new HashMap(200);
105
106
            } else if (qName.equals(XML_NODE_ROOT)) {
107
108
                try {
109
                    fVersion = Integer.parseInt(attributes
110
                            .getValue(XML_ATTRIBUTE_VERSION));
111
                } catch (NumberFormatException ex) {
112
                    throw new SAXException(ex);
113
                }
114
115
            }
116
        }
117
118
        public Map getSettings() {
119
            return fSettings;
120
        }
121
122
        public int getVersion() {
123
            return fVersion;
124
        }
125
126
        public String getName() {
127
            return fName;
128
        }
129
130
    }
131
132
    /**
133
     * Deals with the messages in the properties file (cut n' pasted from a
134
     * generated class).
135
     */
136
    private final static class FormatterAppMessages {
137
        private static final String BUNDLE_NAME = "org.eclipse.jdt.core.formatter.FormatterAppMessages";//$NON-NLS-1$
138
139
        private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
140
                .getBundle(BUNDLE_NAME);
141
142
        public static String getString(String key) {
143
            try {
144
                return RESOURCE_BUNDLE.getString(key);
145
            } catch (MissingResourceException e) {
146
                return '!' + key + '!';
147
            }
148
        }
149
150
        public static String getFormattedString(String key, String arg) {
151
            return getFormattedString(key, new String[] { arg });
152
        }
153
154
        public static String getFormattedString(String key, String[] args) {
155
            return MessageFormat.format(getString(key), args);
156
        }
157
    }
158
159
    /**
160
     * Read the xml config file and return a Map representing the options that
161
     * are in the specified config file.
162
     */
163
    public Map readConfig(String filename) {
164
165
        try {
166
            final FileInputStream reader = new FileInputStream(new File(
167
                    filename));
168
            final ConfigHandler handler = new ConfigHandler();
169
            
170
            try {
171
                InputSource inputSource = new InputSource(reader);
172
                final SAXParserFactory factory = SAXParserFactory.newInstance();
173
                final SAXParser parser = factory.newSAXParser();
174
                parser.parse(inputSource, handler);
175
                if (handler.getVersion() != ProfileVersionNumbers.CURRENT_VERSION)
176
                    return null;
177
                configName = handler.getName();
178
                return handler.getSettings();
179
180
            } finally {
181
                try { reader.close(); } catch (IOException e) { /* ignore */ }
182
            }
183
184
        } catch (IOException e) {
185
            Util.log(e, FormatterAppMessages
186
                    .getString("ConfigFile.reading.error")); //$NON-NLS-1$
187
        } catch (SAXException e) {
188
            Util.log(e, FormatterAppMessages
189
                    .getString("ConfigFile.reading.error")); //$NON-NLS-1$
190
        } catch (ParserConfigurationException e) {
191
            Util.log(e, FormatterAppMessages
192
                    .getString("ConfigFile.reading.error")); //$NON-NLS-1$
193
        }
194
        return null;
195
    }
196
    
197
    /**
198
     * Runs the Java code formatter application
199
     */
200
    public Object run(Object args) throws Exception {
201
        
202
        ArrayList fileList = processCommandLine((String[]) args);
203
        
204
        // nothing to do
205
        if (fileList == null || fileList.isEmpty())
206
        	return EXIT_OK;
207
       
208
        if (!quiet) {
209
        	if (configName != null)
210
        		System.out.println(FormatterAppMessages.getFormattedString("CommandLine.config.file", configName)); //$NON-NLS-1$
211
            System.out.println(FormatterAppMessages.getString("CommandLine.start")); //$NON-NLS-1$
212
        }
213
        
214
        // format the list of files and/or directories
215
        while (!fileList.isEmpty()) {
216
        	File file = (File) fileList.remove(0);
217
        		
218
            if (file.isDirectory())
219
                formatDirTree(file);
220
            else
221
                formatFile(file);    
222
        }
223
        
224
        if (!quiet) {
225
            System.out.println(FormatterAppMessages.getString("CommandLine.done")); //$NON-NLS-1$
226
        }
227
        
228
        return EXIT_OK;
229
    }
230
231
    private void displayHelp(String message) {
232
        System.err.println(message);
233
        System.out.println(""); //$NON-NLS-1$
234
        displayHelp();
235
    }
236
237
    private String configName;
238
    
239
    /*
240
     * The output will look like this:
241
     * 
242
     * "Usage: eclipse -application org.eclipse.jdt.core.JavaCodeFormatter [ OPTIONS ] <files>
243
     *     <files>		Java source files and/or directories to format.
244
     *					Only files ending with .java will be formatted in the given directory.
245
     *  OPTIONS:
246
     *	   -config <file>		Use the formatting style from the specified config file.
247
     *							This file must be an xml file that has been exported by Eclipse 3.0.
248
     *     -help				Display this message.
249
     *     -quiet				Only print error messages.
250
     *	   -verbose				Be verbose about the formatting job.   
251
     */
252
    private void displayHelp() {
253
        String binaryName = Platform.getOS().equals(Platform.OS_WIN32) ? "eclipse.exe" : "eclipse"; //$NON-NLS-1$ //$NON-NLS-2$
254
255
        // this is UG-LY. is there a way to make this look nicer?
256
        System.out.println(FormatterAppMessages.getFormattedString("CommandLine.usage", //$NON-NLS-1$
257
        		binaryName + " -application org.eclipse.jdt.core.JavaCodeFormatter")); //$NON-NLS-1$
258
        System.out.println(""); //$NON-NLS-1$
259
        
260
        System.out.println("   " + FormatterAppMessages.getString("CommandLine.files") //$NON-NLS-1$ //$NON-NLS-2$
261
                + "\t" + FormatterAppMessages.getString("CommandLine.files.msg1")); //$NON-NLS-1$ //$NON-NLS-2$
262
        System.out.println("\t\t" //$NON-NLS-1$
263
        		+ FormatterAppMessages.getFormattedString("CommandLine.files.msg2", ".java")); //$NON-NLS-1$ //$NON-NLS-2$ 
264
        
265
        System.out.println(FormatterAppMessages.getString("CommandLine.options")); //$NON-NLS-1$
266
        System.out.println("   " + FormatterAppMessages.getFormattedString("CommandLine.config", ARG_CONFIG) //$NON-NLS-1$ //$NON-NLS-2$ 
267
        		+ "\t" + FormatterAppMessages.getString("CommandLine.config.msg1")); //$NON-NLS-1$ //$NON-NLS-2$
268
        System.out.println("\t\t\t" + FormatterAppMessages.getString("CommandLine.config.msg2")); //$NON-NLS-1$ //$NON-NLS-2$
269
        System.out.println("   " + ARG_HELP + "\t\t" + FormatterAppMessages.getString("CommandLine.help")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
270
        System.out.println("   " + ARG_QUIET + "\t\t" + FormatterAppMessages.getString("CommandLine.quiet")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
271
        System.out.println("   " + ARG_VERBOSE +"\t\t" + FormatterAppMessages.getString("CommandLine.verbose")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 
272
    }
273
    
274
    private final String ARG_HELP =    "-help"; //$NON-NLS-1$
275
    private final String ARG_CONFIG =  "-config"; //$NON-NLS-1$
276
    private final String ARG_VERBOSE = "-verbose"; //$NON-NLS-1$
277
    private final String ARG_QUIET = "-quiet"; //$NON-NLS-1$
278
    private boolean verbose = false;
279
    private boolean quiet = false;
280
    
281
    private ArrayList processCommandLine(String[] argsArray) {
282
283
        ArrayList args = new ArrayList();
284
        for (int i = 0; i < argsArray.length; i++) {
285
			args.add(argsArray[i]);
286
		}
287
        
288
        // look for flag-like args
289
        if (args.remove(ARG_HELP)) {
290
            displayHelp();
291
            return null;
292
        }
293
        if (args.remove(ARG_VERBOSE))
294
            verbose = true;
295
        if (args.remove(ARG_QUIET)) 
296
        	quiet = true;
297
        
298
        if (quiet && verbose) {
299
        	displayHelp(FormatterAppMessages.getFormattedString
300
        			("CommandLineError.quiet.verbose", new String[] {ARG_QUIET, ARG_VERBOSE})); //$NON-NLS-1$
301
        	return null;
302
        }
303
        args.remove("-pdelaunch"); //$NON-NLS-1$
304
305
        // look for flag/param args
306
        int index = args.indexOf(ARG_CONFIG);
307
        if (index >= 0) {
308
            args.remove(index);
309
            String configFile = (String) args.remove(index);
310
            options = readConfig(configFile);
311
            if (options == null) {
312
                displayHelp(FormatterAppMessages
313
                        .getFormattedString("CommandLineError.config", configFile)); //$NON-NLS-1$
314
                return null;
315
            }
316
        }
317
318
        // only the files and directories should remain
319
        ArrayList fileList = new ArrayList();
320
        while (!args.isEmpty()) {
321
			String fileName = (String) args.remove(0);
322
			File file = new File(fileName);
323
			if (file.exists()) {
324
				fileList.add(file);
325
			} else {
326
				displayHelp(FormatterAppMessages
327
					.getFormattedString("CommandLineError.file", fileName)); //$NON-NLS-1$
328
				return null;
329
			}
330
		}
331
        
332
        if (fileList.isEmpty())
333
        	displayHelp(FormatterAppMessages.getString("CommandLineError.file.dir")); //$NON-NLS-1$
334
        	
335
        return fileList;
336
    }
337
338
    /**
339
     * Recursively format the Java source code that is contained in the
340
     * directory rooted at dir.
341
     */
342
    private void formatDirTree(File dir) {
343
344
        File[] files = dir.listFiles();
345
        if (files == null)
346
            return;
347
348
        for (int i = 0; i < files.length; i++) {
349
            File file = files[i];
350
            if (file.isDirectory()) {
351
                formatDirTree(file);
352
            } else if (file.getPath().endsWith(".java")) { //$NON-NLS-1$
353
                formatFile(file);
354
            }
355
        }
356
    }
357
358
    // internal representation of configuration options in the xml file
359
    private Map options = null;
360
361
    /**
362
     * Format the given Java source file.
363
     */
364
    private void formatFile(File file) {
365
        
366
        IDocument doc = new Document();
367
        try {
368
            // read the file
369
            final BufferedReader in = new BufferedReader(new FileReader(file));
370
            if (verbose) {           
371
                System.out.println(FormatterAppMessages.getFormattedString
372
                		("CommandLine.formatting", file.getName())); //$NON-NLS-1$
373
            }
374
            String line;
375
            String contents = ""; //$NON-NLS-1$
376
            try {
377
                while ((line = in.readLine()) != null)
378
                    contents = contents
379
                            + System.getProperty("line.separator") + line; //$NON-NLS-1$
380
            } finally {
381
                try { in.close(); } catch (IOException e) { /* ignore */  }
382
            }
383
          
384
            // format the file (the meat and potatoes)
385
            doc.set(contents);
386
            TextEdit edit = ToolFactory.createCodeFormatter(options).format(
387
                    CodeFormatter.K_COMPILATION_UNIT, doc.get(), 0,
388
                    doc.getLength(), 0, null);
389
            if (edit != null) {
390
                edit.apply(doc);
391
            } else {            	
392
                System.err.println
393
                	(FormatterAppMessages.getFormattedString("Edit.problem", file.getName())); //$NON-NLS-1$
394
                return;
395
            }
396
        
397
            // write the file
398
            final Writer out = new BufferedWriter(new FileWriter(file, false));
399
            try {
400
                out.write(doc.get());
401
                out.flush();
402
            } finally {
403
                try { out.close(); } catch (IOException e) { /* ignore */ }
404
            }
405
            
406
        } catch (IOException e) {
407
            String errorMessage = FormatterAppMessages.getString("Exception.io") + " " //$NON-NLS-1$ //$NON-NLS-2$
408
            	+ e.getLocalizedMessage();
409
            Util.log(e, errorMessage);
410
            System.err.println(errorMessage); 
411
            System.err.println(FormatterAppMessages.getString("Exception.skip")); //$NON-NLS-1$
412
            
413
        }  catch (BadLocationException e) {
414
            String errorMessage = FormatterAppMessages.getString("Exception.bad.location") + " " //$NON-NLS-1$ //$NON-NLS-2$
415
    			+ e.getLocalizedMessage();
416
            Util.log(e, errorMessage);
417
            System.err.println(errorMessage); 
418
            System.err.println(FormatterAppMessages.getString("Exception.skip")); //$NON-NLS-1$
419
        }
420
    }
421
422
}
(-)ui/org/eclipse/jdt/internal/ui/preferences/formatter/ProfileStore.java (-4 / +5 lines)
Lines 50-55 Link Here
50
import org.eclipse.core.resources.ResourcesPlugin;
50
import org.eclipse.core.resources.ResourcesPlugin;
51
51
52
import org.eclipse.jdt.core.JavaCore;
52
import org.eclipse.jdt.core.JavaCore;
53
import org.eclipse.jdt.core.formatter.ProfileVersionNumbers;
53
54
54
import org.eclipse.jdt.ui.JavaUI;
55
import org.eclipse.jdt.ui.JavaUI;
55
56
Lines 177-183 Link Here
177
			}
178
			}
178
			IEclipsePreferences uiPreferences = instanceScope.getNode(JavaUI.ID_PLUGIN);
179
			IEclipsePreferences uiPreferences = instanceScope.getNode(JavaUI.ID_PLUGIN);
179
			uiPreferences.put(PREF_FORMATTER_PROFILES, val);
180
			uiPreferences.put(PREF_FORMATTER_PROFILES, val);
180
			uiPreferences.putInt(PREF_FORMATTER_PROFILES_VERSION, ProfileVersioner.CURRENT_VERSION);
181
			uiPreferences.putInt(PREF_FORMATTER_PROFILES_VERSION, ProfileVersionNumbers.CURRENT_VERSION);
181
		} finally {
182
		} finally {
182
			try { stream.close(); } catch (IOException e) { /* ignore */ }
183
			try { stream.close(); } catch (IOException e) { /* ignore */ }
183
		}
184
		}
Lines 324-330 Link Here
324
			final Document document= builder.newDocument();
325
			final Document document= builder.newDocument();
325
			
326
			
326
			final Element rootElement = document.createElement(XML_NODE_ROOT);
327
			final Element rootElement = document.createElement(XML_NODE_ROOT);
327
			rootElement.setAttribute(XML_ATTRIBUTE_VERSION, Integer.toString(ProfileVersioner.CURRENT_VERSION));
328
			rootElement.setAttribute(XML_ATTRIBUTE_VERSION, Integer.toString(ProfileVersionNumbers.CURRENT_VERSION));
328
329
329
			document.appendChild(rootElement);
330
			document.appendChild(rootElement);
330
			
331
			
Lines 381-387 Link Here
381
		IScopeContext instanceScope= access.getInstanceScope();
382
		IScopeContext instanceScope= access.getInstanceScope();
382
		IEclipsePreferences uiPreferences= instanceScope.getNode(JavaUI.ID_PLUGIN);
383
		IEclipsePreferences uiPreferences= instanceScope.getNode(JavaUI.ID_PLUGIN);
383
		int version= uiPreferences.getInt(PREF_FORMATTER_PROFILES_VERSION, 0);
384
		int version= uiPreferences.getInt(PREF_FORMATTER_PROFILES_VERSION, 0);
384
		if (version >= ProfileVersioner.CURRENT_VERSION) {
385
		if (version >= ProfileVersionNumbers.CURRENT_VERSION) {
385
			return; // is up to date
386
			return; // is up to date
386
		}
387
		}
387
		try {
388
		try {
Lines 393-399 Link Here
393
			if (manager.getSelected() instanceof CustomProfile) {
394
			if (manager.getSelected() instanceof CustomProfile) {
394
				manager.commitChanges(instanceScope); // updates JavaCore options
395
				manager.commitChanges(instanceScope); // updates JavaCore options
395
			}
396
			}
396
			uiPreferences.putInt(PREF_FORMATTER_PROFILES_VERSION, ProfileVersioner.CURRENT_VERSION);
397
			uiPreferences.putInt(PREF_FORMATTER_PROFILES_VERSION, ProfileVersionNumbers.CURRENT_VERSION);
397
			savePreferences(instanceScope);
398
			savePreferences(instanceScope);
398
						
399
						
399
			IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects();
400
			IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects();
(-)ui/org/eclipse/jdt/internal/ui/preferences/formatter/ProfileManager.java (-7 / +8 lines)
Lines 28-33 Link Here
28
28
29
import org.eclipse.jdt.core.JavaCore;
29
import org.eclipse.jdt.core.JavaCore;
30
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
30
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
31
import org.eclipse.jdt.core.formatter.ProfileVersionNumbers;
31
32
32
import org.eclipse.jdt.internal.corext.util.Messages;
33
import org.eclipse.jdt.internal.corext.util.Messages;
33
34
Lines 63-69 Link Here
63
		public abstract void setSettings(Map settings);
64
		public abstract void setSettings(Map settings);
64
		
65
		
65
		public int getVersion() {
66
		public int getVersion() {
66
			return ProfileVersioner.CURRENT_VERSION;
67
			return ProfileVersionNumbers.CURRENT_VERSION;
67
		}
68
		}
68
		
69
		
69
		public boolean hasEqualSettings(Map otherMap, List allKeys) {
70
		public boolean hasEqualSettings(Map otherMap, List allKeys) {
Lines 119-125 Link Here
119
		
120
		
120
		public Profile rename(String name, ProfileManager manager) {
121
		public Profile rename(String name, ProfileManager manager) {
121
			final String trimmed= name.trim();
122
			final String trimmed= name.trim();
122
		 	CustomProfile newProfile= new CustomProfile(trimmed, fSettings, ProfileVersioner.CURRENT_VERSION);
123
		 	CustomProfile newProfile= new CustomProfile(trimmed, fSettings, ProfileVersionNumbers.CURRENT_VERSION);
123
		 	manager.addProfile(newProfile);
124
		 	manager.addProfile(newProfile);
124
			return newProfile;
125
			return newProfile;
125
		}
126
		}
Lines 235-241 Link Here
235
	public final static class SharedProfile extends CustomProfile {
236
	public final static class SharedProfile extends CustomProfile {
236
		
237
		
237
		public SharedProfile(String oldName, Map options) {
238
		public SharedProfile(String oldName, Map options) {
238
			super(oldName, options, ProfileVersioner.CURRENT_VERSION);
239
			super(oldName, options, ProfileVersionNumbers.CURRENT_VERSION);
239
		}
240
		}
240
		
241
		
241
		public Profile rename(String name, ProfileManager manager) {
242
		public Profile rename(String name, ProfileManager manager) {
Lines 463-470 Link Here
463
		IEclipsePreferences uiPrefs= context.getNode(JavaUI.ID_PLUGIN);
464
		IEclipsePreferences uiPrefs= context.getNode(JavaUI.ID_PLUGIN);
464
		IEclipsePreferences corePrefs= context.getNode(JavaCore.PLUGIN_ID);
465
		IEclipsePreferences corePrefs= context.getNode(JavaCore.PLUGIN_ID);
465
				
466
				
466
		int version= uiPrefs.getInt(FORMATTER_SETTINGS_VERSION, ProfileVersioner.VERSION_1);
467
		int version= uiPrefs.getInt(FORMATTER_SETTINGS_VERSION, ProfileVersionNumbers.VERSION_1);
467
		if (version != ProfileVersioner.CURRENT_VERSION) {
468
		if (version != ProfileVersionNumbers.CURRENT_VERSION) {
468
			Map allOptions= new HashMap();
469
			Map allOptions= new HashMap();
469
			addAll(uiPrefs, allOptions);
470
			addAll(uiPrefs, allOptions);
470
			addAll(corePrefs, allOptions);
471
			addAll(corePrefs, allOptions);
Lines 556-563 Link Here
556
		final IEclipsePreferences uiPrefs= context.getNode(JavaUI.ID_PLUGIN);
557
		final IEclipsePreferences uiPrefs= context.getNode(JavaUI.ID_PLUGIN);
557
		updatePreferences(uiPrefs, fUIKeys, profileOptions);
558
		updatePreferences(uiPrefs, fUIKeys, profileOptions);
558
		
559
		
559
		if (uiPrefs.getInt(FORMATTER_SETTINGS_VERSION, 0) != ProfileVersioner.CURRENT_VERSION) {
560
		if (uiPrefs.getInt(FORMATTER_SETTINGS_VERSION, 0) != ProfileVersionNumbers.CURRENT_VERSION) {
560
			uiPrefs.putInt(FORMATTER_SETTINGS_VERSION, ProfileVersioner.CURRENT_VERSION);
561
			uiPrefs.putInt(FORMATTER_SETTINGS_VERSION, ProfileVersionNumbers.CURRENT_VERSION);
561
		}
562
		}
562
		
563
		
563
		if (context.getName() == InstanceScope.SCOPE) {
564
		if (context.getName() == InstanceScope.SCOPE) {
(-)ui/org/eclipse/jdt/internal/ui/preferences/formatter/CreateProfileDialog.java (-1 / +3 lines)
Lines 35-40 Link Here
35
import org.eclipse.jface.dialogs.IDialogSettings;
35
import org.eclipse.jface.dialogs.IDialogSettings;
36
import org.eclipse.jface.dialogs.StatusDialog;
36
import org.eclipse.jface.dialogs.StatusDialog;
37
37
38
import org.eclipse.jdt.core.formatter.ProfileVersionNumbers;
39
38
import org.eclipse.jdt.ui.JavaUI;
40
import org.eclipse.jdt.ui.JavaUI;
39
41
40
import org.eclipse.jdt.internal.ui.JavaPlugin;
42
import org.eclipse.jdt.internal.ui.JavaPlugin;
Lines 186-192 Link Here
186
		final Map baseSettings= new HashMap(((Profile)fSortedProfiles.get(fProfileCombo.getSelectionIndex())).getSettings());
188
		final Map baseSettings= new HashMap(((Profile)fSortedProfiles.get(fProfileCombo.getSelectionIndex())).getSettings());
187
		final String profileName= fNameText.getText();
189
		final String profileName= fNameText.getText();
188
		
190
		
189
		fCreatedProfile= new CustomProfile(profileName, baseSettings, ProfileVersioner.CURRENT_VERSION);
191
		fCreatedProfile= new CustomProfile(profileName, baseSettings, ProfileVersionNumbers.CURRENT_VERSION);
190
		fProfileManager.addProfile(fCreatedProfile);
192
		fProfileManager.addProfile(fCreatedProfile);
191
		super.okPressed();
193
		super.okPressed();
192
	}
194
	}
(-)ui/org/eclipse/jdt/internal/ui/preferences/formatter/ProfileVersioner.java (-21 / +11 lines)
Lines 16-44 Link Here
16
16
17
import org.eclipse.jdt.core.JavaCore;
17
import org.eclipse.jdt.core.JavaCore;
18
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
18
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
19
import org.eclipse.jdt.core.formatter.ProfileVersionNumbers;
19
20
20
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
21
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
22
21
import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile;
23
import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile;
22
24
23
25
24
public class ProfileVersioner {
26
public class ProfileVersioner {
25
	
27
	
26
	public static final int VERSION_1= 1; // < 20040113 (includes M6)
27
	public static final int VERSION_2= 2; // before renaming almost all
28
	public static final int VERSION_3= 3; // after renaming almost all
29
	public static final int VERSION_4= 4; 
30
	public static final int VERSION_5= 5; // after splitting of FORMATTER_INDENT_BLOCK_STATEMENTS
31
	public static final int VERSION_6= 6; // after splitting of new_line_in_control_statements
32
	public static final int VERSION_7= 7; // after moving comment formatter to JDT Core
33
	public static final int VERSION_8= 8; // fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=89739
34
	public static final int VERSION_9= 9; // after storing project profile names in preferences
35
	
36
	public static final int CURRENT_VERSION= VERSION_9;
37
	
38
	public static void updateAndComplete(CustomProfile profile) {
28
	public static void updateAndComplete(CustomProfile profile) {
39
		final Map oldSettings= profile.getSettings();
29
		final Map oldSettings= profile.getSettings();
40
		Map newSettings= updateAndComplete(oldSettings, profile.getVersion());
30
		Map newSettings= updateAndComplete(oldSettings, profile.getVersion());
41
		profile.setVersion(CURRENT_VERSION);
31
		profile.setVersion(ProfileVersionNumbers.CURRENT_VERSION);
42
		profile.setSettings(newSettings);
32
		profile.setSettings(newSettings);
43
	}
33
	}
44
	
34
	
Lines 47-68 Link Here
47
		
37
		
48
		switch (version) {
38
		switch (version) {
49
39
50
		case VERSION_1:
40
		case ProfileVersionNumbers.VERSION_1:
51
			version1to2(oldSettings);
41
			version1to2(oldSettings);
52
			
42
			
53
		case VERSION_2:
43
		case ProfileVersionNumbers.VERSION_2:
54
			version2to3(oldSettings);
44
			version2to3(oldSettings);
55
			
45
			
56
		case VERSION_3:
46
		case ProfileVersionNumbers.VERSION_3:
57
		    version3to4(oldSettings);
47
		    version3to4(oldSettings);
58
		    
48
		    
59
		case VERSION_4:
49
		case ProfileVersionNumbers.VERSION_4:
60
		    version4to5(oldSettings);
50
		    version4to5(oldSettings);
61
		    
51
		    
62
		case VERSION_5:
52
		case ProfileVersionNumbers.VERSION_5:
63
		    version5to6(oldSettings);
53
		    version5to6(oldSettings);
64
			
54
			
65
		case VERSION_6:
55
		case ProfileVersionNumbers.VERSION_6:
66
		    version6to7(oldSettings);
56
		    version6to7(oldSettings);
67
		    
57
		    
68
		default:
58
		default:
Lines 117-125 Link Here
117
107
118
	public static int getVersionStatus(CustomProfile profile) {
108
	public static int getVersionStatus(CustomProfile profile) {
119
		final int version= profile.getVersion();
109
		final int version= profile.getVersion();
120
		if (version < CURRENT_VERSION) 
110
		if (version < ProfileVersionNumbers.CURRENT_VERSION) 
121
			return -1;
111
			return -1;
122
		else if (version > CURRENT_VERSION)
112
		else if (version > ProfileVersionNumbers.CURRENT_VERSION)
123
			return 1;
113
			return 1;
124
		else 
114
		else 
125
			return 0;
115
			return 0;

Return to bug 75333