Skip to main content
summaryrefslogtreecommitdiffstats
blob: 17ad692e7d3addd8b4b424a8deb76ebe3f295614 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.ote.ui.test.manager.models;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.ui.ws.AWorkspace;
import org.eclipse.osee.ote.ui.test.manager.internal.TestManagerPlugin;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

public class FileModel {

   private File file = null;
   private IFile iFile = null;
   private long lastModified = 0;
   private String path = "";
   private String rawFilename = "";
   private String text = null;

   public FileModel(String rawFilename) {
      this.rawFilename = rawFilename;
      if (getIFile() != null) {
         lastModified = getIFile().getModificationStamp();
      }
   }

   public boolean exists() {
      return getFile().exists();
   }

   /**
    * @return Returns the file.
    */
   public File getFile() {
      if (file == null) {
         file = new File(rawFilename);
      }
      return file;
   }

   /**
    * @return Returns the iFile for the given local data {@link #rawFilename}.  You may still have to check if the file actually exists.
    */
   public IFile getIFile() {
      if (iFile == null) {
         if (path.equals("")) {
            if (!rawFilename.equals("")) {
               iFile = AWorkspace.getIFile(rawFilename);
               if(iFile == null){
                  IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(org.eclipse.core.filesystem.URIUtil.toURI(rawFilename));
                  if(files.length > 0){
                     iFile = files[0];                     
                  }
               }
            }
         }
      }
      return iFile;
   }

   /**
    * @return Returns the name.
    */
   public String getName() {
      return new File(rawFilename).getName();
   }

   /**
    * @return Returns the path.
    */
   public String getPath() {
      if (iFile == null) {
         iFile = getIFile();
      }
      if (iFile != null) {
         path = iFile.getFullPath().toString();
      }
      return path;
   }

   /**
    * @return Returns the rawFilename.
    */
   public String getRawFilename() {
      return rawFilename;
   }

   public String getWorkspaceRelativePath() {
      IWorkspace ws = ResourcesPlugin.getWorkspace();
      IFile ifile = ws.getRoot().getFileForLocation(new Path(rawFilename));
      if(ifile == null){
         IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(org.eclipse.core.filesystem.URIUtil.toURI(rawFilename));
         if(files.length > 0){
            ifile = files[0];
         }
      }
      if (!ifile.exists()) {
         return null;
      } else {
         return ifile.getFullPath().toString();
      }
   }

   public String getText() throws IOException {
      if (iFile == null) {
         getIFile();
      }
      if (iFile == null) {
         return "";
      }
      if (text == null || iFile.getModificationStamp() != lastModified) {
         text = Lib.fileToString(new File(rawFilename));
         OseeLog.log(TestManagerPlugin.class, Level.INFO, "getText: Reading file " + getName());
      } else {
         OseeLog.log(TestManagerPlugin.class, Level.INFO, "getText: Using buffered file " + getName());
      }
      lastModified = iFile.getModificationStamp();
      return text;
   }

   public boolean isModified() {
      if (iFile == null) {
         getIFile();
      }
      if (iFile == null) {
         OseeLog.log(TestManagerPlugin.class, Level.WARNING, "Can't Read iFile");
         return true;
      }
      return iFile.getModificationStamp() != lastModified;
   }

   public void openEditor() {
      if (getIFile() != null) {
         AWorkspace.openEditor(getIFile());
      } else {
         IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(file.getAbsolutePath()));
         IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
         try {
            IDE.openEditorOnFileStore(page, fileStore);
         } catch (PartInitException e) {
            e.printStackTrace();
         }         
      }
   }

   public void openPackageExplorer() {
      OseeLog.log(TestManagerPlugin.class, Level.INFO, "Show in explorer " + getName());
      // Open in Package Explorer and error if can't
      boolean success = AWorkspace.showInPackageExplorer(getIFile());
      //      if(!success){
      //         success = AWorkspace.showInResourceNavigator(getIFile());
      //      }
      if (!success) {
         MessageDialog.openInformation(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Open Error",
            "Can't Show in Explorer\n\n" + getName());
      }
      // As a convenience, open in Navigator, but don't error
      success = AWorkspace.showInResourceNavigator(getIFile());
   }

   /**
    * @param path The path to set.
    */
   public void setPath(String path) {
      this.path = path;
   }

   /**
    * @param rawFilename The rawFilename to set.
    */
   public void setRawFilename(String rawFilename) {
      this.rawFilename = rawFilename;
   }
}

Back to the top