~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/ImageSourceEditor/ChooseImageDialog.xaml.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.Collections.ObjectModel;
 
7
using System.ComponentModel;
 
8
using System.IO;
 
9
using System.Linq;
 
10
using System.Windows;
 
11
using System.Windows.Data;
 
12
 
 
13
using ICSharpCode.Core;
 
14
using ICSharpCode.SharpDevelop.Project;
 
15
using Microsoft.Win32;
 
16
 
 
17
namespace ICSharpCode.WpfDesign.AddIn.ImageSourceEditor
 
18
{
 
19
        /// <summary>
 
20
        /// Dialog which allows user to add images to the project
 
21
        /// </summary>
 
22
        public partial class ChooseImageDialog : Window
 
23
        {
 
24
                /// <summary>
 
25
                /// Contains the allowed extensions for image files.
 
26
                /// </summary>
 
27
                static readonly string[] Extension = {".jpg", ".bmp", ".png", ".gif", ".ico", ".dib", ".jpe", ".jpeg", ".tif", ".tiff"};
 
28
                
 
29
                private ObservableCollection<ImageData> _data = new ObservableCollection<ImageData>();
 
30
                
 
31
                public ChooseImageDialog()
 
32
                {
 
33
                        InitializeComponent();
 
34
                        
 
35
                        // Get image file with allowed extensions
 
36
                        AddImages(ProjectTools.RetrieveFiles(ChooseImageDialog.Extension));
 
37
                        
 
38
                        CollectionViewSource cvs = new CollectionViewSource();
 
39
                        cvs.Source = _data;
 
40
                        cvs.GroupDescriptions.Add(new PropertyGroupDescription("Directory"));
 
41
                        cvs.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
 
42
                        imgDisplay.ItemsSource = cvs.View;
 
43
                }
 
44
                
 
45
                List<ImageData> AddImages(IEnumerable<FileProjectItem> files)
 
46
                {
 
47
                        List<ImageData> images = new List<ImageData>();
 
48
                        foreach (FileProjectItem file in files) {
 
49
                                ImageData image = new ImageData(file.FileName, Path.GetDirectoryName(file.VirtualName) + Path.DirectorySeparatorChar);
 
50
                                images.Add(image);
 
51
                                _data.Add(image);
 
52
                        }
 
53
                        return images;
 
54
                }
 
55
                
 
56
                #region Event Handlers
 
57
                private void AddClick(object sender, RoutedEventArgs e)
 
58
                {
 
59
                        OpenFileDialog dialog = new OpenFileDialog();
 
60
                        dialog.Filter = "Image Files|*" + String.Join(";*",Extension) + "|All Files|*.*";
 
61
                        dialog.Multiselect = true;
 
62
                        dialog.CheckFileExists = true;
 
63
                        dialog.Title = "Choose Image";
 
64
                        
 
65
                        if (dialog.ShowDialog() == true) {
 
66
                                string[] fileNames = dialog.FileNames;
 
67
                                var files = ProjectTools.AddFiles(fileNames, ItemType.Resource);
 
68
                                imgDisplay.SelectedItem = files.FirstOrDefault();
 
69
                        }
 
70
                }
 
71
                
 
72
                private void OkClick(object sender, RoutedEventArgs e)
 
73
                {
 
74
                        DialogResult = true;
 
75
                        Close();
 
76
                }
 
77
                
 
78
                private void ImageDisplayDoubleClick(object sender, RoutedEventArgs e)
 
79
                {
 
80
                        DialogResult = true;
 
81
                        Close();
 
82
                }
 
83
                
 
84
                public string SelectedFileName {
 
85
                        get {
 
86
                                var image = (ImageData)imgDisplay.SelectedItem;
 
87
                                if (image != null) {
 
88
                                        return image.FullName;
 
89
                                } else {
 
90
                                        return null;
 
91
                                }
 
92
                        }
 
93
                        set {
 
94
                                imgDisplay.SelectedItem = _data.FirstOrDefault(d => FileUtility.IsEqualFileName(d.FullName, value));
 
95
                        }
 
96
                }
 
97
                
 
98
                private void Cancel(object sender, RoutedEventArgs e)
 
99
                {
 
100
                        DialogResult = false;
 
101
                        Close();
 
102
                }
 
103
                #endregion
 
104
        }
 
105
        
 
106
        public class ImageData
 
107
        {
 
108
                /// <summary>
 
109
                /// The directory (relative to project root) where the image is stored.
 
110
                /// </summary>
 
111
                public string Directory { get; private set; }
 
112
                
 
113
                /// <summary>
 
114
                /// The short file name.
 
115
                /// </summary>
 
116
                public string Name { get; private set; }
 
117
                
 
118
                /// <summary>
 
119
                /// The full file name.
 
120
                /// </summary>
 
121
                public string FullName { get; private set; }
 
122
                
 
123
                public ImageData(string fullName, string relDirectory)
 
124
                {
 
125
                        this.FullName = fullName;
 
126
                        this.Name = Path.GetFileName(fullName);
 
127
                        this.Directory = relDirectory;
 
128
                }
 
129
        }
 
130
}