代码直接上:

入口类

  1. import java.io.File;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.json.JSONArray;
  5. import org.json.JSONObject;
  6. public class MainPane {
  7. public static void main(String[] args) throws Throwable {
  8. File file = new File("acl.properties");
  9. ProperityUtils.loadProperty2System(file.getAbsolutePath());
  10. //System.out.println(System.getProperty("username"));
  11. String userId = System.getProperty("username","user");
  12. String password = System.getProperty("password","pwd");
  13. String url = System.getProperty("url","http://127.0.0.1/web");
  14. String json = ""
  15. String session = Http.sendPost(url, json);
  16. JSONObject jsonobject = new JSONObject(session);
  17. String sid = jsonobject.getString("sessionId");
  18. String user = jsonobject.getString("id");
  19. String stationId = jsonobject.getString("stationId");
  20. String getGroupUrl = url;
  21. System.out.println(getGroupUrl);
  22. String groups = Http.sendGet(getGroupUrl);
  23. JSONArray array = new JSONArray(groups);
  24. System.out.println(array.length());
  25. List<FolderMo> mo = new ArrayList<FolderMo>();
  26. for (int i = 0; i < array.length(); i++) {
  27. FolderMo fm = new FolderMo(array.getJSONObject(i).getString("name"),
  28. array.getJSONObject(i).getString("id"),
  29. "");
  30. mo.add(fm);
  31. }
  32. //启动面板
  33. new ShowPane(sid, stationId, url).start(mo);
  34. }
  35. }

展示类:

  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseListener;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Vector;
  11. import javax.swing.Box;
  12. import javax.swing.JFrame;
  13. import javax.swing.JMenuItem;
  14. import javax.swing.JPopupMenu;
  15. import javax.swing.JScrollPane;
  16. import javax.swing.JTable;
  17. import javax.swing.JTree;
  18. import javax.swing.event.TreeSelectionEvent;
  19. import javax.swing.event.TreeSelectionListener;
  20. import javax.swing.table.DefaultTableModel;
  21. import javax.swing.tree.DefaultMutableTreeNode;
  22. import javax.swing.tree.TreePath;
  23. import org.json.JSONArray;
  24. import org.json.JSONObject;
  25. public class ShowPane implements MouseListener, ActionListener{
  26. JPopupMenu popMenu;// 右键菜单
  27. JMenuItem addItem;// 添加
  28. JMenuItem delItem;// 删除
  29. JMenuItem editItem;// 修改
  30. JPopupMenu treePopMenu; //树菜单
  31. JMenuItem moveItem;// 移动到上一级
  32. JTable table;
  33. JTree tree;
  34. public String sid;
  35. public String stationId;
  36. public String url;
  37. Map<String,String> map = new HashMap<String, String>();
  38. Map<String,Integer> rowMap = new HashMap<String, Integer>();
  39. public ShowPane(String sid, String stationId, String url) {
  40. this.sid = sid;
  41. this.stationId = stationId;
  42. this.url = url;
  43. System.out.println(this.url+"="+this.sid+"="+this.stationId);
  44. popMenu = new JPopupMenu();
  45. addItem = new JMenuItem("添加");
  46. addItem.addActionListener(this);
  47. delItem = new JMenuItem("删除");
  48. delItem.addActionListener(this);
  49. editItem = new JMenuItem("提交");
  50. editItem.addActionListener(this);
  51. popMenu.add(addItem);
  52. popMenu.add(delItem);
  53. popMenu.add(editItem);
  54. treePopMenu = new JPopupMenu();
  55. moveItem = new JMenuItem("移动到上一级");
  56. moveItem.addActionListener(this);
  57. treePopMenu.add(moveItem);
  58. }
  59. public void start(List<FolderMo> folder) {
  60. DefaultMutableTreeNode top = new DefaultMutableTreeNode(this.stationId);
  61. DefaultMutableTreeNode node = new DefaultMutableTreeNode("");
  62. for (FolderMo folderMo : folder) {
  63. node = new DefaultMutableTreeNode(folderMo);
  64. top.add(node);
  65. }
  66. tree = new JTree(top);
  67. Vector<String> columnNames = new Vector<String>(); //设置列名
  68. columnNames.add("id");
  69. columnNames.add("type");
  70. columnNames.add("privileges");
  71. Vector<Vector<Object>> rowData = new Vector<Vector<Object>>();
  72. //Vector<Object> hang = new Vector<Object>();//设置每一行的值
  73. //rowData.add(hang);//加入rowData中
  74. DefaultTableModel tablemodel = new DefaultTableModel(rowData,columnNames);
  75. table = new JTable(tablemodel);
  76. /* TableColumn column = table.getColumnModel().getColumn(1);
  77. column.setMinWidth(100);
  78. column.setMaxWidth(300);
  79. column.setPreferredWidth(150);
  80. column = table.getColumnModel().getColumn(2);
  81. column.setMinWidth(300);
  82. column.setMaxWidth(500);
  83. column.setPreferredWidth(400);*/
  84. JFrame f = new JFrame("文件夹权限(运维人员使用)");
  85. Box box = Box.createHorizontalBox(); //创建Box 类对象
  86. //f.add(tree);
  87. JScrollPane treePane = new JScrollPane(tree) {
  88. /**
  89. *
  90. */
  91. private static final long serialVersionUID = 1L;
  92. @Override
  93. public Dimension getPreferredSize() {
  94. return new Dimension(200, 600);
  95. }
  96. };
  97. box.add(treePane, BorderLayout.WEST);
  98. box.add(new JScrollPane(table), BorderLayout.EAST);
  99. f.getContentPane().add(box, BorderLayout.CENTER);
  100. f.setLocation(100, 100);
  101. f.setSize(700, 700);
  102. f.setVisible(true);
  103. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  104. // 添加选择事件
  105. tree.addTreeSelectionListener(new TreeSelectionListener() {
  106. @Override
  107. public void valueChanged(TreeSelectionEvent e) {
  108. DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
  109. .getLastSelectedPathComponent();
  110. if (node == null)
  111. return;
  112. Object object = node.getUserObject();
  113. FolderMo folder = (FolderMo) object;
  114. System.out.println("你选择了:" + folder.showString());
  115. map.put("folderId", folder.getId());
  116. //更新表格
  117. updateTable(folder);
  118. //更新树
  119. updateTree(folder,node);
  120. }
  121. protected void updateTree(FolderMo folder,DefaultMutableTreeNode node) {
  122. String fs = getFolderList(url, sid, folder.getId());
  123. if("".equals(fs)) {
  124. System.out.println(folder.getName()+"下未查询到子文件夹");
  125. }else {
  126. if(node.isLeaf()) {
  127. node.removeAllChildren();
  128. }
  129. JSONArray array = new JSONArray(fs);
  130. for (int i = 0; i < array.length(); i++) {
  131. FolderMo fm = new FolderMo(array.getJSONObject(i).getString("name"),
  132. array.getJSONObject(i).getString("id"),
  133. array.getJSONObject(i).get("acl").toString());
  134. DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(fm);
  135. node.add(node2);
  136. }
  137. tree.expandPath(new TreePath(node));
  138. }
  139. }
  140. protected void updateTable(FolderMo folder) {
  141. String acl = folder.getAcl();
  142. if("".equals(acl)) {
  143. return;
  144. }
  145. tablemodel.getDataVector().clear();
  146. JSONArray acls = new JSONArray(acl);
  147. for (int i = 0; i < acls.length(); i++) {
  148. Vector<Object> hang = new Vector<Object>();//设置每一行的值
  149. hang.add(acls.getJSONObject(i).getString("id"));
  150. hang.add(acls.getJSONObject(i).getString("type"));
  151. hang.add(acls.getJSONObject(i).get("privileges").toString());
  152. rowData.add(hang);//加入rowData中
  153. }
  154. tablemodel.setDataVector(rowData, columnNames);
  155. table.updateUI();
  156. }
  157. });
  158. tree.addMouseListener(new MouseListener() {
  159. @Override
  160. public void mouseReleased(MouseEvent e) {
  161. // TODO Auto-generated method stub
  162. }
  163. @Override
  164. public void mousePressed(MouseEvent e) {
  165. TreePath path = tree.getPathForLocation(e.getX(), e.getY());
  166. if (path == null) {
  167. return;
  168. }
  169. //System.out.println("当前目录"+path.toString());
  170. if(path.getPath().length < 3) {
  171. System.out.println("当前目录"+path+"不允许上移");
  172. return;
  173. }
  174. tree.setSelectionPath(path);
  175. if (e.getButton() == 3) {
  176. treePopMenu.show(tree, e.getX(), e.getY());
  177. }
  178. }
  179. @Override
  180. public void mouseExited(MouseEvent e) {
  181. // TODO Auto-generated method stub
  182. }
  183. @Override
  184. public void mouseEntered(MouseEvent e) {
  185. // TODO Auto-generated method stub
  186. }
  187. @Override
  188. public void mouseClicked(MouseEvent e) {
  189. // TODO Auto-generated method stub
  190. }
  191. });
  192. table.addMouseListener(new MouseListener() {
  193. @Override
  194. public void mouseReleased(MouseEvent e) {
  195. // TODO Auto-generated method stub
  196. }
  197. @Override
  198. public void mousePressed(MouseEvent e) {
  199. //获取鼠标右键选中的行
  200. int row = table.rowAtPoint(e.getPoint());
  201. if (row == -1) {
  202. rowMap.remove("row");
  203. return ;
  204. }else {
  205. rowMap.put("row", row);
  206. }
  207. //获取已选中的行
  208. int[] rows = table.getSelectedRows();
  209. boolean inSelected = false ;
  210. //判断当前右键所在行是否已选中
  211. for(int r : rows){
  212. if(row == r){
  213. inSelected = true ;
  214. break ;
  215. }
  216. }
  217. //当前鼠标右键点击所在行不被选中则高亮显示选中行
  218. if(!inSelected){
  219. table.setRowSelectionInterval(row, row);
  220. }
  221. // TODO Auto-generated method stub
  222. if (e.getButton() == 3) {
  223. popMenu.show(table, e.getX(), e.getY());
  224. }
  225. }
  226. @Override
  227. public void mouseExited(MouseEvent e) {
  228. // TODO Auto-generated method stub
  229. }
  230. @Override
  231. public void mouseEntered(MouseEvent e) {
  232. // TODO Auto-generated method stub
  233. }
  234. @Override
  235. public void mouseClicked(MouseEvent e) {
  236. // TODO Auto-generated method stub
  237. }
  238. });
  239. }
  240. @Override
  241. public void actionPerformed(ActionEvent e) {
  242. // TODO Auto-generated method stub
  243. if(e.getSource() == moveItem) {
  244. DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
  245. if (node == null)
  246. return;
  247. Object object = node.getUserObject();
  248. FolderMo folder = (FolderMo) object;
  249. String moIds = folder.getId();
  250. System.out.println("当前节点:" + folder.showString());
  251. node = (DefaultMutableTreeNode)node.getParent();
  252. object = node.getUserObject();
  253. folder = (FolderMo) object;
  254. String targetFolderId = folder.getId();
  255. System.out.println("当前节点的父节点:" + folder.showString());
  256. String result = moveFolderToUp(moIds,targetFolderId);
  257. if("".equals(result)) {
  258. tree.collapsePath(new TreePath(node)); //关闭
  259. }else {
  260. System.out.println("移动文件夹失败"+result);
  261. }
  262. return;
  263. }
  264. DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
  265. if (e.getActionCommand().equals("添加")) {
  266. tableModel.addRow(new Object[] { "", "Group", "[\"refRead\",\"write\",\"delete\",\"read\",\"editAcl\",\"create\",\"list\"]" });
  267. }
  268. if (e.getActionCommand().equals("删除")) {
  269. //tableModel.removeRow(rowMap.get("row"));// rowIndex是要删除的行序号
  270. int[] rows = table.getSelectedRows();
  271. for (int i = 0; i < rows.length; i++) {
  272. int row = rows[i]-i;
  273. tableModel.removeRow(row);// rowIndex是要删除的行序号
  274. }
  275. }
  276. if (e.getActionCommand().equals("提交")) {
  277. // 更新acl
  278. System.out.println("文件夹Id:" + map.get("folderId"));
  279. int rowCount = tableModel.getRowCount();
  280. AclMo[] acls = new AclMo[rowCount];
  281. for (int i = 0; i < rowCount; i++) {
  282. AclMo mo = new AclMo();
  283. mo.setId((String)tableModel.getValueAt(i, 0));// 取得第i行第一列的数据
  284. mo.setType((String)tableModel.getValueAt(i, 1));// 取得第i行第二列的数据
  285. String privileges = (String)tableModel.getValueAt(i, 2);
  286. mo.setPrivileges(privileges.replace("[", "").replace("]", "").replaceAll("\"", ""));// 取得第i行第三列的数据
  287. acls[i] = mo;
  288. }
  289. System.out.println("更新权限请求参数如下:");
  290. System.out.println(JSONObject.valueToString(acls));
  291. String result = updateAcl(map.get("folderId"),JSONObject.valueToString(acls));
  292. if("".equals(result)) {
  293. System.out.println(map.get("folderId") + "更新权限成功");
  294. }else {
  295. System.out.println(map.get("folderId") +"更新权限失败:"+result);
  296. }
  297. }
  298. }
  299. @Override
  300. public void mouseClicked(MouseEvent e) {
  301. // TODO Auto-generated method stub
  302. }
  303. @Override
  304. public void mousePressed(MouseEvent e) {
  305. //菜单被按下
  306. }
  307. @Override
  308. public void mouseReleased(MouseEvent e) {
  309. // TODO Auto-generated method stub
  310. }
  311. @Override
  312. public void mouseEntered(MouseEvent e) {
  313. // TODO Auto-generated method stub
  314. }
  315. @Override
  316. public void mouseExited(MouseEvent e) {
  317. // TODO Auto-generated method stub
  318. }
  319. public String updateAcl(String moId, String acls) {
  320. String result = Http.sendPost2(updateAclUrl,acls);
  321. return result;
  322. }
  323. public String moveFolderToUp(String moIds, String targetFolderId) {
  324. String result = Http.sendPost2(moveFolderurl,"");
  325. return result;
  326. }
  327. public static String getFolderList(String url, String sid, String parentId) {
  328. String folders = Http.sendGet(getFolderurl);
  329. return folders;
  330. }
  331. }

工具类:

  1. 1
  2. https://repo1.maven.org/maven2/org/json/json/20190722/json-20190722.jar
  3.  
  4. 2
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.IOException;
  8. import java.util.Iterator;
  9. import java.util.Properties;
  10. public class ProperityUtils {
  11. public static String loadProperty2System(String location) throws IOException {
  12. File file = new File(location);
  13. if (file.isFile() && file.getName().endsWith(".properties")) {
  14. Properties ppt = new Properties();
  15. FileInputStream fi = new FileInputStream(file);
  16. Throwable e = null;
  17. try {
  18. ppt.load(fi);
  19. Iterator<String> iterator = (Iterator<String>) ppt.stringPropertyNames().iterator();
  20. while (iterator.hasNext()) {
  21. String key = iterator.next();
  22. System.setProperty(key, ppt.getProperty(key));
  23. // if (!System.getProperties().containsKey(key)) {
  24. // }
  25. }
  26. } catch (Throwable e2) {
  27. e = e2;
  28. throw e2;
  29. } finally {
  30. if (fi != null) {
  31. if (e != null) {
  32. try {
  33. fi.close();
  34. } catch (Throwable e3) {
  35. e.addSuppressed(e3);
  36. }
  37. } else {
  38. fi.close();
  39. }
  40. }
  41. }
  42. } else {
  43. System.out.println("“" + location + "”,Can not load any property file.");
  44. }
  45. return file.getAbsolutePath();
  46. }
  47. }
  48. 3
  49. import java.io.BufferedOutputStream;
  50. import java.io.BufferedReader;
  51. import java.io.DataOutputStream;
  52. import java.io.IOException;
  53. import java.io.InputStreamReader;
  54. import java.io.PrintWriter;
  55. import java.net.HttpURLConnection;
  56. import java.net.MalformedURLException;
  57. import java.net.URL;
  58. public class Http {
  59. public static String sendPost2(String u, String param) {
  60. StringBuffer sbf = new StringBuffer();
  61. try {
  62. URL url = new URL(u);
  63. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  64. connection.setDoInput(true);
  65. connection.setDoOutput(true);
  66. connection.setRequestMethod("POST");
  67. connection.setUseCaches(false);
  68. connection.setInstanceFollowRedirects(true);
  69. connection.addRequestProperty("Content-Type", "application/json");
  70. connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  71. connection.connect();
  72. DataOutputStream out = new DataOutputStream(connection.getOutputStream());
  73. if (!"".equals(param)) {
  74. // 正文,正文内容其实跟get的URL中 '? '后的参数字符串一致
  75. // String content = "字段名=" + URLEncoder.encode("字符串值", "编码");
  76. out.writeBytes(param);
  77. }
  78. out.flush();
  79. out.close();
  80. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  81. String lines;
  82. while ((lines = reader.readLine()) != null) {
  83. lines = new String(lines.getBytes(), "utf-8");
  84. sbf.append(lines);
  85. }
  86. System.out.println(sbf);
  87. reader.close();
  88. // 断开连接
  89. connection.disconnect();
  90. } catch (MalformedURLException e) {
  91. // TODO Auto-generated catch block
  92. e.printStackTrace();
  93. } catch (IOException e) {
  94. // TODO Auto-generated catch block
  95. e.printStackTrace();
  96. }
  97. return sbf.toString();
  98. }
  99. /**
  100. * 发送http POST请求
  101. *
  102. * @param
  103. * @return 远程响应结果
  104. */
  105. public static String sendPost(String u, String json) {
  106. StringBuffer sbf = new StringBuffer();
  107. try {
  108. URL url = new URL(u);
  109. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  110. connection.setDoInput(true);
  111. connection.setDoOutput(true);
  112. connection.setRequestMethod("POST");
  113. connection.setUseCaches(false);
  114. connection.setInstanceFollowRedirects(true);
  115. connection.addRequestProperty("role", "Admin");
  116. connection.addRequestProperty("Content-Type", "application/json");
  117. connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  118. connection.connect();
  119. DataOutputStream out = new DataOutputStream(connection.getOutputStream());
  120. if (!"".equals(json)) {
  121. out.writeBytes(json);
  122. }
  123. out.flush();
  124. out.close();
  125. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  126. String lines;
  127. while ((lines = reader.readLine()) != null) {
  128. lines = new String(lines.getBytes(), "utf-8");
  129. sbf.append(lines);
  130. }
  131. System.out.println(sbf);
  132. reader.close();
  133. // 断开连接
  134. connection.disconnect();
  135. } catch (MalformedURLException e) {
  136. // TODO Auto-generated catch block
  137. e.printStackTrace();
  138. } catch (IOException e) {
  139. // TODO Auto-generated catch block
  140. e.printStackTrace();
  141. }
  142. return sbf.toString();
  143. }
  144. public static String sendGet(String u) {
  145. StringBuffer sbf = new StringBuffer();
  146. try {
  147. URL url = new URL(u);
  148. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  149. connection.setDoInput(true); // 设置可输入
  150. connection.setDoOutput(true); // 设置该连接是可以输出的
  151. connection.setRequestMethod("GET"); // 设置请求方式
  152. connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
  153. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  154. String lines;
  155. while ((lines = reader.readLine()) != null) {
  156. lines = new String(lines.getBytes(), "utf-8");
  157. sbf.append(lines);
  158. }
  159. System.out.println(sbf);
  160. reader.close();
  161. // 断开连接
  162. connection.disconnect();
  163. } catch (MalformedURLException e) {
  164. // TODO Auto-generated catch block
  165. e.printStackTrace();
  166. } catch (IOException e) {
  167. // TODO Auto-generated catch block
  168. e.printStackTrace();
  169. }
  170. return sbf.toString();
  171. }
  172. /**
  173. * 解决编码问题
  174. * @param uri
  175. * @param data
  176. */
  177. public void sendGet2(String uri,String data) {
  178. try {
  179. URL url = new URL(uri);
  180. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  181. connection.setDoInput(true); // 设置可输入
  182. connection.setDoOutput(true); // 设置该连接是可以输出的
  183. connection.setRequestMethod("GET"); // 设置请求方式
  184. connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
  185. PrintWriter pw = new PrintWriter(new BufferedOutputStream(connection.getOutputStream()));
  186. pw.write(data);
  187. pw.flush();
  188. pw.close();
  189. BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
  190. String line = null;
  191. StringBuilder result = new StringBuilder();
  192. while ((line = br.readLine()) != null) { // 读取数据
  193. result.append(line + "\n");
  194. }
  195. connection.disconnect();
  196. System.out.println(result.toString());
  197. } catch (Exception e) {
  198. e.printStackTrace();
  199. }
  200. }
  201. }

4、model:

  1. public class FolderMo {
  2. private String name;
  3. private String id;
  4. private String acl;
  5. public FolderMo(String n) {
  6. name = n;
  7. }
  8. public FolderMo(String n, String id,String acl) {
  9. name = n;
  10. this.id = id;
  11. this.acl = acl;
  12. }
  13. public FolderMo() {
  14. }
  15. // 重点在toString,节点的显示文本就是toString
  16. public String toString() {
  17. return name;
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. public String getId() {
  26. return id;
  27. }
  28. public void setId(String id) {
  29. this.id = id;
  30. }
  31. public String getAcl() {
  32. return acl;
  33. }
  34. public void setAcl(String acl) {
  35. this.acl = acl;
  36. }
  37. public String showString() {
  38. return id+"-"+name;
  39. }
  40. }
  1. public class AclMo {
  2. private String id;
  3. private String type;
  4. private String privileges;
  5. public String getId() {
  6. return id;
  7. }
  8. public void setId(String id) {
  9. this.id = id;
  10. }
  11. public String getType() {
  12. return type;
  13. }
  14. public void setType(String type) {
  15. this.type = type;
  16. }
  17. public String getPrivileges() {
  18. return privileges;
  19. }
  20. public void setPrivileges(String privileges) {
  21. this.privileges = privileges;
  22. }
  23. }

 

版权声明:本文为liangblog原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/liangblog/p/13976525.html