PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/edu/uncc/parsets/util/osabstraction/Windows.java

https://code.google.com/p/parsets/
Java | 124 lines | 65 code | 23 blank | 36 comment | 4 complexity | ba5ce1563915c76075b87cc19816794b MD5 | raw file
  1package edu.uncc.parsets.util.osabstraction;
  2
  3import java.util.HashMap;
  4import java.util.Map;
  5
  6import javax.swing.UIManager;
  7
  8import com.sun.jna.Library;
  9import com.sun.jna.Native;
 10import com.sun.jna.NativeMapped;
 11import com.sun.jna.PointerType;
 12import com.sun.jna.win32.W32APIFunctionMapper;
 13import com.sun.jna.win32.W32APITypeMapper;
 14
 15import edu.uncc.parsets.util.PSLogging;
 16
 17/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
 18 * Copyright (c) 2009, Robert Kosara, Caroline Ziemkiewicz,
 19 *                     and others (see Authors.txt for full list)
 20 * All rights reserved.
 21 * 
 22 * Redistribution and use in source and binary forms, with or without
 23 * modification, are permitted provided that the following conditions are met:
 24 * 
 25 *    * Redistributions of source code must retain the above copyright
 26 *      notice, this list of conditions and the following disclaimer.
 27 *    * Redistributions in binary form must reproduce the above copyright
 28 *      notice, this list of conditions and the following disclaimer in the
 29 *      documentation and/or other materials provided with the distribution.
 30 *    * Neither the name of UNC Charlotte nor the names of its contributors
 31 *      may be used to endorse or promote products derived from this software
 32 *      without specific prior written permission.
 33 *      
 34 * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS ''AS IS'' AND ANY
 35 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 36 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 37 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 38 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 39 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 40 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 41 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 43 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 44\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 45
 46public class Windows extends AbstractOS {
 47
 48	public Windows() {
 49		try {
 50			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
 51		} catch (Exception e) {
 52			PSLogging.logger.info("Could not set Windows look and feel", e);
 53		}
 54	}
 55	
 56	// http://stackoverflow.com/questions/585534/what-is-the-best-way-to-find-the-users-home-directory-in-java
 57	// https://jna.dev.java.net/
 58	@Override
 59	public String getLocalDBDir() {
 60		if (com.sun.jna.Platform.isWindows()) {
 61			HWND hwndOwner = null;
 62			int nFolder = Shell32.CSIDL_APPDATA;
 63			HANDLE hToken = null;
 64			int dwFlags = Shell32.SHGFP_TYPE_CURRENT;
 65			char[] pszPath = new char[Shell32.MAX_PATH];
 66			int hResult = Shell32.INSTANCE.SHGetFolderPath(hwndOwner, nFolder, hToken, dwFlags, pszPath);
 67			if (Shell32.S_OK == hResult) {
 68				String path = new String(pszPath);
 69				int len = path.indexOf('\0');
 70				path = path.substring(0, len);
 71				return path;
 72			} else {
 73				PSLogging.logger.fatal("Error determining Application Data directory: "+hResult);
 74				return null;
 75			}
 76		} else
 77			return null;
 78	}
 79
 80    private static Map<String, Object> OPTIONS = new HashMap<String, Object>();
 81	static {
 82		OPTIONS.put(Library.OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE);
 83		OPTIONS.put(Library.OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.UNICODE);
 84	}
 85
 86	static class HANDLE extends PointerType implements NativeMapped {
 87	}
 88
 89	static class HWND extends HANDLE {
 90	}
 91
 92	static interface Shell32 extends Library {
 93
 94		public static final int MAX_PATH = 260;
 95
 96		public static final int CSIDL_LOCAL_APPDATA = 0x001c;
 97		
 98		public static final int CSIDL_APPDATA = 0x001a;
 99		
100		public static final int CSIDL_PERSONAL = 0x0005;
101
102		public static final int SHGFP_TYPE_CURRENT = 0;
103
104		public static final int SHGFP_TYPE_DEFAULT = 1;
105
106		public static final int S_OK = 0;
107
108		static Shell32 INSTANCE = (Shell32) Native.loadLibrary("shell32", Shell32.class, OPTIONS);
109
110		/**
111		 * see http://msdn.microsoft.com/en-us/library/bb762181(VS.85).aspx
112		 * 
113		 * HRESULT SHGetFolderPath( HWND hwndOwner, int nFolder, HANDLE hToken,
114		 * DWORD dwFlags, LPTSTR pszPath);
115		 */
116		public int SHGetFolderPath(HWND hwndOwner, int nFolder, HANDLE hToken, int dwFlags, char[] pszPath);
117	}
118
119	@Override
120	public String shortName() {
121		return "win";
122	}
123
124}