• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.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
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
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.
15  */
16 
17 package com.android.ide.eclipse.adt.internal.editors.layout;
18 
19 import com.android.ide.eclipse.adt.internal.resources.manager.ResourceFolder;
20 import com.android.ide.eclipse.adt.internal.resources.manager.ResourceFolderType;
21 import com.android.ide.eclipse.adt.internal.resources.manager.ResourceManager;
22 
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.ui.IEditorInput;
25 import org.eclipse.ui.IEditorMatchingStrategy;
26 import org.eclipse.ui.IEditorReference;
27 import org.eclipse.ui.PartInitException;
28 import org.eclipse.ui.part.FileEditorInput;
29 
30 /**
31  * Matching strategy for the Layout Editor. This is used to open all configurations of a layout
32  * in the same editor.
33  */
34 public class MatchingStrategy implements IEditorMatchingStrategy {
35 
matches(IEditorReference editorRef, IEditorInput input)36     public boolean matches(IEditorReference editorRef, IEditorInput input) {
37         // first check that the file being opened is a layout file.
38         if (input instanceof FileEditorInput) {
39             FileEditorInput fileInput = (FileEditorInput)input;
40 
41             // get the IFile object and check it's in one of the layout folders.
42             IFile iFile = fileInput.getFile();
43             ResourceFolder resFolder = ResourceManager.getInstance().getResourceFolder(iFile);
44 
45             // if it's a layout, we know check the name of the fileInput against the name of the
46             // file being currently edited by the editor since those are independent of the config.
47             if (resFolder != null && resFolder.getType() == ResourceFolderType.LAYOUT) {
48                 try {
49                     IEditorInput editorInput = editorRef.getEditorInput();
50                     if (editorInput instanceof FileEditorInput) {
51                         FileEditorInput editorFileInput = (FileEditorInput)editorInput;
52                         IFile editorIFile = editorFileInput.getFile();
53 
54                         return editorIFile.getProject().equals(iFile.getProject())
55                             && editorIFile.getName().equals(iFile.getName());
56                     }
57                 } catch (PartInitException e) {
58                     // we do nothing, we'll just return false.
59                 }
60             }
61         }
62         return false;
63     }
64 }
65