/* 
 * Copyright (c) 2012 Bruno Barbieri
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list
 * of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

package com.googlecode.jmkvpropedit;

import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.text.*;

public class Utils {
		
	/* Start of OS detection functions */
	
	public static boolean isWindows() {
		String OS = System.getProperty("os.name");
		
		if (OS.toLowerCase().startsWith("windows"))
			return true;
		else
			return false;
	}
	
	public static boolean isMac() {
		String OS = System.getProperty("os.name");
		
		if (OS.toLowerCase().startsWith("mac"))
			return true;
		else
			return false;
	}
	
	public static boolean isLinux() {
		String OS = System.getProperty("os.name");
		
		if (OS.toLowerCase().startsWith("linux"))
			return true;
		else
			return false;
	}
	
	/* End of OS detection functions */
	
	
	/* Start of escaping functions */
	
	public static String escapeName (String name) {
		if (!name.isEmpty()) {
			name = name.replace("\\","\\\\");
			name = name.replace(" ", "\\s");
			name = name.replace("\"","\\2");
			name = name.replace(":","\\c");
			name = name.replace("#","\\h");
		}
		
		return name;
	}
	
	public static String escapeNameCmdLine (String name) {
		name = name.replace("\"", "\\\"");
		
		return name;
	}
	
	public static String escapeBackslashes (String text) {
		text = text.replace("\\", "\\\\");
		
		return text;
	}
	
	public static String escapePath (String path) {
		if (Utils.isWindows()) {
			path = path.replace("\\", "\\\\");
		} else {
			path = path.replace("\\", "\\\\");
			path = path.replace(" ", "\\ ");
			path = path.replace("[", "\\[");
			path = path.replace("]", "\\]");
			path = path.replace("{", "\\{");
			path = path.replace("}", "\\}");
			path = path.replace("(", "\\(");
			path = path.replace(")", "\\)");
			path = path.replace("<", "\\<");
			path = path.replace(">", "\\>");
			path = path.replace("\'", "\\\'");
			path = path.replace("\"", "\\\"");
			path = path.replace("&", "\\&");
			path = path.replace("*", "\\*");
			path = path.replace("?", "\\?");
			path = path.replace("|", "\\|");
			path = path.replace(":", "\\:");
			path = path.replace(";", "\\;");
		}
		
		return path;
	}
	
	/* End of escaping functions */
	
	
	/* Start of right-click menu code */
	
	private static void showRCMenu(JTextComponent text, MouseEvent e) {
		JPopupMenu rightClickMenu = new JPopupMenu();
		JMenuItem copyMenuItem = new JMenuItem(text.getActionMap().get(DefaultEditorKit.copyAction));
		JMenuItem cutMenuItem = new JMenuItem(text.getActionMap().get(DefaultEditorKit.cutAction));
		JMenuItem pasteMenuItem = new JMenuItem(text.getActionMap().get(DefaultEditorKit.pasteAction));
		JMenuItem selectAllMenuItem = new JMenuItem(text.getActionMap().get(DefaultEditorKit.selectAllAction));
		
		copyMenuItem.setText("Copy");
		cutMenuItem.setText("Cut");
		pasteMenuItem.setText("Paste");
		selectAllMenuItem.setText("Select All");

		rightClickMenu.add(copyMenuItem);
		rightClickMenu.add(cutMenuItem);
		rightClickMenu.add(pasteMenuItem);
		rightClickMenu.addSeparator();
		rightClickMenu.add(selectAllMenuItem);
		
		if (text.getText().isEmpty()) {
			copyMenuItem.setEnabled(false);
			selectAllMenuItem.setEnabled(false);
			cutMenuItem.setEnabled(false);
		}
		
		if (text.getSelectionStart() == text.getSelectionEnd()) {
			copyMenuItem.setEnabled(false);
			cutMenuItem.setEnabled(false);
		}
		
		if ((text.getSelectionStart()+text.getSelectionEnd()) == text.getText().length()) {
			selectAllMenuItem.setEnabled(false);
		}
		
		if (!text.isEditable()) {
			cutMenuItem.setEnabled(false);
			pasteMenuItem.setEnabled(false);
		}
		
		rightClickMenu.show(text, e.getX(), e.getY());
	}
	
	public static void addRCMenuMouseListener(final JTextComponent text) {
		text.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				if (e.isMetaDown() && text.isEnabled()) {
					text.requestFocus();
					showRCMenu(text, e);
				}
			}
		});
	}
	
	/* End of right-click menu code */
	
	
	public static NumberFormat padNumber(int pad) {
		NumberFormat formatter = new DecimalFormat("0");
		if (pad > 0) {
			String n = "";
			for (int i = 0; i < pad; i++) {
				n += 0;
			}
			formatter = new DecimalFormat(n);
		}
		return formatter;
	}
	
	public static String getFileNameWithoutExt(String file) {
		int dotIndex = file.lastIndexOf(".");
		
		return file.substring(0, dotIndex);
	}
}

@@ -5304,28 +5304,22 @@
  static 
-NumberFormat
+String
  padNumb
@@ -5328,16 +5328,28 @@
 (int pad
+, int number
 ) %7B%0A%09%09Nu
@@ -5395,16 +5395,19 @@
 t(%220%22);%0A
+%09%09%0A
 %09%09if (pa
@@ -5526,16 +5526,19 @@
 n);%0A%09%09%7D%0A
+%09%09%0A
 %09%09return
@@ -5547,16 +5547,31 @@
 ormatter
+.format(number)
 ;%0A%09%7D%0A%09%0A%09

Simplify (i.e. fix) number padding method