BrightSide Workbench Full Report + Source Code
FileClipboard.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.file.zul;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.apache.commons.io.FileUtils;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.elephant.util.Messages;
28 import org.turro.zul.frame.Framework;
29 
34 public class FileClipboard {
35 
36  public static void paste(File folder) {
37  pasteFile(folder);
38  pasteFolder(folder);
40  }
41 
42  public static void copyFile(File value) {
44  Framework.getClipboard().put("bba_clip_file_copy", value);
45  }
46 
47  public static void cutFile(String name, final File value) {
48  Messages.confirmCutting().add(name).show(() -> {
50  Framework.getClipboard().put("bba_clip_file_cut", value);
51  });
52  }
53 
54  private static void pasteFile(File folder) {
55  boolean cut = false;
56  File file = (File) Framework.getClipboard().get("bba_clip_file_copy");
57  if (file == null) {
58  file = (File) Framework.getClipboard().get("bba_clip_file_cut");
59  cut = true;
60  }
61  if (file != null) {
62  try {
63  if(cut) {
64  FileUtils.moveFile(file, new File(folder.getAbsolutePath() + "/" + file.getName()));
65  } else {
66  FileUtils.copyFile(file, new File(folder.getAbsolutePath() + "/" + file.getName()));
67  }
68  } catch (IOException ex) {
69  Logger.getLogger(FileClipboard.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
70  }
71  }
72  }
73 
74  public static void copyFolder(File value) {
76  Framework.getClipboard().put("bba_clip_file_folder_copy", value);
77  }
78 
79  public static void cutFolder(String name, final File value) {
80  Messages.confirmCutting().add(name).show(() -> {
82  Framework.getClipboard().put("bba_clip_file_folder_cut", value);
83  });
84  }
85 
86  private static void pasteFolder(File folder) {
87  boolean cut = false;
88  File origin = (File) Framework.getClipboard().get("bba_clip_file_folder_copy");
89  if(origin == null) {
90  origin = (File) Framework.getClipboard().get("bba_clip_file_folder_cut");
91  cut = true;
92  }
93  if(origin != null) {
94  try {
95  if(cut) {
96  FileUtils.moveDirectory(origin, folder);
97  } else {
98  FileUtils.copyDirectory(origin, folder);
99  }
100  } catch (IOException ex) {
101  Logger.getLogger(FileClipboard.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
102  }
103  }
104  }
105 
106  public static boolean hasClipboard() {
107  return Framework.getClipboard().get("bba_clip_file_folder_copy") != null ||
108  Framework.getClipboard().get("bba_clip_file_folder_cut") != null ||
109  Framework.getClipboard().get("bba_clip_file_copy") != null ||
110  Framework.getClipboard().get("bba_clip_file_cut") != null;
111  }
112 
113  public static void clearClipboard() {
114  Framework.getClipboard().remove("bba_clip_file_folder_copy");
115  Framework.getClipboard().remove("bba_clip_file_folder_cut");
116  Framework.getClipboard().remove("bba_clip_file_copy");
117  Framework.getClipboard().remove("bba_clip_file_cut");
118  }
119 
120  private FileClipboard() {
121  }
122 
123 }
static Messages confirmCutting()
Definition: Messages.java:91
Messages add(String word)
Definition: Messages.java:50
static void copyFolder(File value)
static void paste(File folder)
static void cutFile(String name, final File value)
static void cutFolder(String name, final File value)
static void copyFile(File value)
static FrameClipboard getClipboard()
Definition: Framework.java:220