/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

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