]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/runtime/org.argeo.eclipse.ui.rcp/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java
8b48a630733da2f9c74c7f21a6bfcafd58b8da74
2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.eclipse
.ui
.specific
;
18 import java
.awt
.Desktop
;
20 import java
.io
.FileOutputStream
;
21 import java
.io
.IOException
;
22 import java
.io
.InputStream
;
23 import java
.io
.OutputStream
;
25 import org
.apache
.commons
.io
.IOUtils
;
26 import org
.argeo
.ArgeoException
;
29 * Abstraction that enable to implement runtime environment specific (typically
30 * RCP or RAP) methods while dealing with files in the UI.
33 public class FileHandler
{
35 private FileProvider provider
;
37 public FileHandler(FileProvider provider
) {
38 this.provider
= provider
;
41 public void openFile(String fileName
, String fileId
) {
42 String tmpFileName
= fileName
;
43 String prefix
= "", extension
= "";
44 if (fileName
!= null) {
45 int ind
= fileName
.lastIndexOf('.');
47 prefix
= fileName
.substring(0, ind
);
48 extension
= fileName
.substring(ind
);
52 InputStream is
= null;
54 is
= provider
.getInputStreamFromFileId(fileId
);
55 File file
= createTmpFile(prefix
, extension
, is
);
56 tmpFileName
= file
.getName();
57 Desktop desktop
= null;
58 if (Desktop
.isDesktopSupported()) {
59 desktop
= Desktop
.getDesktop();
62 } catch (IOException e
) {
63 // Note : tmpFileName = fileName if the error has been thrown while
64 // creating the tmpFile.
65 throw new ArgeoException("Cannot open file " + tmpFileName
, e
);
67 IOUtils
.closeQuietly(is
);
71 private File
createTmpFile(String prefix
, String suffix
, InputStream is
) {
73 OutputStream os
= null;
75 tmpFile
= File
.createTempFile(prefix
, suffix
);
76 os
= new FileOutputStream(tmpFile
);
78 } catch (IOException e
) {
79 throw new ArgeoException("Cannot open file " + prefix
+ "."
82 IOUtils
.closeQuietly(os
);