PageRenderTime 53ms CodeModel.GetById 45ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/structure/SpecToggler.java

#
Java | 141 lines | 106 code | 16 blank | 19 comment | 28 complexity | df59c9063bfd78748dcbbbba7cdae973 MD5 | raw file
  1/*
  2 * RubyActions.java - Actions for Ruby plugin
  3 *
  4 * Copyright 2008 Robert McKinnon
  5 *
  6 * This program is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU General Public License
  8 * as published by the Free Software Foundation; either version 2
  9 * of the License, or any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program; if not, write to the Free Software
 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 19 */
 20package org.jedit.ruby.structure;
 21
 22import org.gjt.sp.jedit.Buffer;
 23import org.gjt.sp.jedit.View;
 24import org.gjt.sp.jedit.jEdit;
 25
 26import java.io.File;
 27import java.io.FilenameFilter;
 28import java.util.HashMap;
 29import java.util.Map;
 30
 31public class SpecToggler {
 32
 33    public static final Map<File, File> SPEC_TO_CODE = new HashMap<File, File>();
 34    public static final Map<File, File> CODE_TO_SPEC = new HashMap<File, File>();
 35
 36    private File currentFile;
 37    private View view;
 38    private String bufferDirectory;
 39    private String bufferName;
 40
 41    public SpecToggler(View theView) {
 42        view = theView;
 43        Buffer buffer = view.getBuffer();
 44        bufferDirectory = buffer.getDirectory();
 45        bufferName = buffer.getName();
 46        currentFile = new File(bufferDirectory, buffer.getName());
 47    }
 48
 49    public void toggleSpec() {
 50        if (isBufferSpecFile()) {
 51            toggleToCode();
 52        } else {
 53            toggleToSpec();
 54        }
 55    }
 56
 57    private void toggleToCode() {
 58        if (SPEC_TO_CODE.containsKey(currentFile)) {
 59            openFile(SPEC_TO_CODE.get(currentFile));
 60        } else {
 61            String[] parts = bufferDirectory.split(File.separatorChar + "spec" + File.separatorChar);
 62            File baseDir = new File(parts[0]);
 63            String subPath = parts[1];
 64            String codeFileName = bufferName.replaceFirst("_spec\\.rb", ".rb");
 65
 66            File code = tryToOpenFile(baseDir, subPath, codeFileName);
 67            if (code == null) {
 68                subPath = "app" + File.separatorChar + subPath;
 69                code = tryToOpenFile(baseDir, subPath, codeFileName);
 70                if (code == null) {
 71                    code = tryToOpenFile(baseDir, subPath, bufferName.replaceFirst("_spec\\.rb", ""));
 72                }
 73            }
 74            if (code != null) {
 75                SPEC_TO_CODE.put(currentFile, code);
 76                CODE_TO_SPEC.put(code, currentFile);
 77            }
 78        }
 79    }
 80
 81    private void toggleToSpec() {
 82        if (CODE_TO_SPEC.containsKey(currentFile)) {
 83            openFile(CODE_TO_SPEC.get(currentFile));
 84        } else {
 85            File specDirectory = getSpecDirectory();
 86            if (specDirectory != null) {
 87                String subPath = bufferDirectory.replaceFirst(specDirectory.getParent(), "");
 88                String specFileName = bufferName.replaceFirst("\\.rb$","_spec.rb");
 89
 90                File spec = tryToOpenFile(specDirectory, subPath, specFileName);
 91                if (spec == null) {
 92                    subPath = subPath.replaceFirst("^/app","");
 93                    spec = tryToOpenFile(specDirectory, subPath, specFileName);
 94                    if (spec == null) {
 95                        spec = tryToOpenFile(specDirectory, subPath, bufferName + "_spec.rb");
 96                    }
 97                }
 98                if (spec != null) {
 99                    SPEC_TO_CODE.put(spec, currentFile);
100                    CODE_TO_SPEC.put(currentFile, spec);
101                }
102            }
103        }
104    }
105
106    private File tryToOpenFile(File directory, String subPath, String fileName) {
107        File file = new File(new File(directory.getPath(), subPath), fileName);
108
109        if (file.exists() && file.isFile()) {
110            openFile(file);
111            return file;
112        } else {
113            return null;
114        }
115    }
116
117    private void openFile(File file) {
118        jEdit.openFile(view, file.getAbsolutePath());
119    }
120
121    private File getSpecDirectory() {
122        File parent = new File(bufferDirectory);
123        File specDir = null;
124        while ((parent = parent.getParentFile()) != null) {
125            String[] files = parent.list(new FilenameFilter() {
126                public boolean accept(File dir, String name) {
127                    return name.equals("spec");
128                }
129            });
130            if (files != null && files.length > 0) {
131                specDir = new File(parent.getPath(), files[0]);
132                break;
133            }
134        }
135        return specDir;
136    }
137
138    private boolean isBufferSpecFile() {
139        return bufferName.contains("_spec");
140    }
141}